Documentation
  • Introduction
  • The Platform
    • Create Application
    • View Builder
    • Report Builder
    • UI Templates
    • Client Policies
    • Client Script
    • Properties
    • Scripting Console
    • Routes
    • Server Policies
    • Server Scrips
    • Scheduled Jobs
    • Data Aggregation
    • Fields
    • Field Dropdowns
    • Collections
    • Sample Application
    • Debugging
  • Reference
    • Introduction
    • API
    • Functions
    • Errors
  • SOFOS EDU-TECH
    • Introduction
    • Reports
    • API
  • PELATIS ITSM
    • Introduction
  • Apps
    • USER MANAGEMENT APP
    • Order Management
      • Client
        • OrderManagement
        • AllOrders
        • ProductManagement
          • ProductTable
          • ProductForm
      • Server Scripts
        • Order
        • Product
      • API
        • Orders
        • Products
Powered by GitBook
On this page

Was this helpful?

  1. Apps
  2. Order Management
  3. Server Scripts

Product

Below is the Product server script. In this example we are using mongoose ORM library for database operations. This server script creates and initiates the Product Schema.

```javascript```
const mongoose = ob.mongoose;
const { Schema, model, SchemaTypes } = mongoose;
const { String, Boolean, Date, Number } = SchemaTypes;

const stockSchema = new mongoose.Schema({
  price: Number,
  quantity: Number
});

const initiate = () => {
  const ProductSchema = new Schema({
    title: String,
    sku: String,
    name: {
      type: String,
      required: [true, "please provide a name for this product"],
      trim: true,
      lowercase: true,
      unique: [true, "name must be unique"],
      minLength: [3, "name must be at least 3 character"],
      maxLength: [20]
    },
    description: {
      type: String,
      required: true
    },
    category: [
      {
        type: String
      }
    ],
    stock: [stockSchema],
    type: String,
    last_stock_price: Number,
    total_quantity: {
      type: Number,
      default: function() {
        const quantity = this.stock.reduce((acc, value) => {
          return (acc += value.quantity);
        }, 0);
        return quantity;
      }
    },
    unit: String,
    created_on: Date,
    updated_on: Date,
    created_by: String,
    updated_by: String,
    domain: String
  });

  try {
    // ob.log(mongoose.modelNames());
    mongoose.connection.deleteModel("Product");
  } catch (e) {}

  ProductSchema.pre("save", function(next) {
    const doc = this;
    ob.log("XXXXXXXXXXXX" + JSON.stringify(doc));
    doc.created_on = new Date();
    doc.updated_on = new Date();
    ob.sequence("om_products", doc.domain, seq => {
      doc.sku = seq;
      next();
    });
  });

  return model("Product", ProductSchema);
};

module.exports.Product = initiate();
PreviousOrderNextAPI

Last updated 1 year ago

Was this helpful?