ouhammmourachid / e-commerce

Public

an e commerce template

just README

  • users10 fields
  • _settings5 fields
  • categories2 fields
  • colors3 fields
  • order_items8 fields
  • orders17 fields
  • payments6 fields
  • products15 fields
  • sizes2 fields
  • subscribers3 fields
  • tags2 fields
  • available_categories3 fields
  • available_colors4 fields
  • available_tags3 fields
  • inventory_sales5 fields
  • products_sales5 fields
  • total_profit2 fields
  • total_sales2 fields
products.pb.js
/// <reference path="../pb_data/types.d.ts" />

onRecordCreate((e) => {
    const record = e.record;

    // Auto-generate product ID with prefix 669557 + 9-digit incrementing number (total 15 chars)
    const prefix = "669557";

    try {
        // Find all products with this prefix and get the highest ID
        const products = $app.findRecordsByFilter(
            "products",
            `id ~ '${prefix}'`,
            "-id", // Order by id descending
            100 // Limit to last 100 products with this prefix
        );

        let maxNumber = 0;

        if (products && products.length > 0) {
            // Find the maximum numeric part from all IDs
            products.forEach((product) => {
                const productId = product.getString("id");
                if (productId && productId.startsWith(prefix)) {
                    const numericPart = parseInt(productId.substring(prefix.length));
                    if (!isNaN(numericPart) && numericPart > maxNumber) {
                        maxNumber = numericPart;
                    }
                }
            });
        }

        // Increment and create new ID
        const nextNumber = maxNumber + 1;
        const newId = prefix + String(nextNumber).padStart(9, '0');
        record.set("id", newId);
        // check if status is empty then set it to draft
        if (!record.get("status")) {
            record.set("status", "draft");
        }
    } catch (error) {
        // If query fails, try to generate a random-ish ID to avoid conflicts
        const timestamp = Date.now() % 1000000000; // Last 9 digits of timestamp
        const newId = prefix + String(timestamp).padStart(9, '0');
        record.set("id", newId);
    }

    const price = parseFloat(record.get("price")) || 0;
    const salePrice = parseFloat(record.get("salePrice")) || 0;

    // Check if price is smaller than sale price
    if (salePrice > 0 && price < salePrice) {
        throw new Error("Price cannot be smaller than Sale Price.");
    }

    // Calculate discount as percentage and round to nearest integer
    if (price > 0 && salePrice > 0 && price > salePrice) {
        const discountPercentage = ((price - salePrice) / price) * 100;
        record.set("discount", Math.round(discountPercentage));
    } else {
        record.set("discount", 0);
    }

    e.next();
}, "products");

onRecordUpdate((e) => {
    const record = e.record;
    const price = parseFloat(record.get("price")) || 0;
    const salePrice = parseFloat(record.get("salePrice")) || 0;

    // Check if price is smaller than sale price
    if (salePrice > 0 && price < salePrice) {
        throw new Error("Price cannot be smaller than Sale Price.");
    }

    // Calculate discount as percentage and round to nearest integer
    if (price > 0 && salePrice > 0 && price > salePrice) {
        const discountPercentage = ((price - salePrice) / price) * 100;
        record.set("discount", Math.round(discountPercentage));
    } else {
        record.set("discount", 0);
    }

    e.next();
}, "products");