# Core API Structure (NodeJS)

# Folder structure

  • It's a NodeJS project, the folders structure is up to the developer, but preferably MVC, like the parent project.
  • The main Js file of each module should have the name main.js.

# Class structure

  • The module is a Js file that has a class that should extends the ModuleBase Class.
  • Except the must implement attributes and methods
Attributes Utility
cartTypes The cart items types that this Module will handle, each type should have one Module to handle
Static Methods Utility
onLoad(context, app) To load the Module (At the server's startup, it's a synchronous function)
async socketOnStart(socket, global_data) Socket start
async socketSubscriptions(socket) Socket start (For subscriptions)
loadModels(models) Load the module'sModels
loadControllers(controllers) Load the module's Controllers
loadMiddlewares(middlewares) Load the module's Middlewares
loadExcludedApiRoutesFromAuth(routes) Load the Excluded API Routes from Auth
loadApiRoutes(routes) Load the Api Routes
loadWebRoutes(routes) Load the Web Routes
Methods (Per request) Utility
async addItemToCart(cart, item) Add an Item to the Cart, by a user interaction or an API call. Should be under this form
async getLinksForSiteMap(baseUrl) Should return an array of {url: String, updated_at: String} for Search engines
async removeItemFromCartAfterCheckout(cart, item) Cleaning the cart after checkout (Quantity management should be here)
async restoreItemQuantityAfterOrderCancelation(order, item) In case order is canceled, to undo the removeItemFromCartAfterCheckout() effect
async cartItemTransform() Return the item for external use, Should be under this form
async onDraftOrderCreation() On Draft Order Creation Trigger
async on(key, payload) On custom events, Check all possible events

# Cart Item Form

For DataBase saving

{
	id: String,
	quantity: Number,
	type: String,
	data: {
		note: String OR Object,
		noteMultiOptionsEnabled: Boolean,
		name: String,
		image: Object,
		compare_at_price: String,
		unit_price: String,
	},
}

# Cart Item JSON Form

API returned, Or for rendering

{
	quantity: Number,
	type: String,
	error: String,
	possible_quantities: Array of [Number],
	_price: Number,
	price: String,
	data: {
		note: String OR Object,
		noteMultiOptionsEnabled: Boolean,
		name: String,
		image: Object,
		compare_at_price: String,
		unit_price: String,
	},
}

# Emitted events

  • order-completed -> { order }
  • add-item-to-cart -> { cart, item }
  • update-item-quantity-in-cart -> { index, quantity }
  • delete-item-from-cart -> { index }