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

Order

How to create a Server Script

A Server Script is a reusable server side function or class which can called inside an API Route.

In order to create a Server Script, follow these steps: -

  1. On navigation, under Scripts, go to Server Script. You will see a list of all the Server Scripts.

  2. Click on ‘settings icon’ on top left corner and then click ‘new’.

  3. A form appears where you have to provide the following details: -

  4. Name - name of the Server Script.

  5. Active – Check / Uncheck if ready / not ready to used.

  6. Script – The code (Business Logic Class).

  7. Click on ‘create’ button. That’s it, you have created the Server Script.

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

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

const statusSchema = new mongoose.Schema({
  state: String,
  update_on: Date
});

const orderLineSchema = new mongoose.Schema({
  sku: String,
  description: String,
  quantity: Number,
  price: Number
});

const initiate = () => {
  const OrderSchema = new Schema({
    sequence: String,
    status: [statusSchema],
    orderLines: [orderLineSchema],
    transactionDate: Date,
    customerName: String,
    customerMobile: String,
    location: String,
    totalAmount: Number,
    created_on: Date,
    updated_on: Date,
    created_by: String,
    updated_by: String,
    domain: String
  });

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

  OrderSchema.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_orders", doc.domain, seq => {
      doc.sequence = seq;
      next();
    });
  });

  return model("om_order", OrderSchema);
};

module.exports.Order = initiate();
PreviousServer ScriptsNextProduct

Last updated 1 year ago

Was this helpful?