BackendService - packet push example

The following example illustrates how to run server-side code on packet push.

import { ApplicationBackendServiceBase, BackendServiceEventTypes } from '@dms';
import { MyEvent } from '@dms-bo';
export * from '@dms';
export * from '@dms-bo';
export default class BackendService extends ApplicationBackendServiceBase {
public async processEvent(
event: { eventName: string;
eventArguments: {
appArea: string;
appCode: string;
userName: string;
request: {
httpMethod: string;
restCommand: string;
headers: any;
body: any;
queryStringParameters: any;
};
};
}): Promise<any> {
switch (event.eventName) {
case "system:packet:push": {
const packetContent = event.eventArguments.request.body;
if (!packetContent) {
return;
}
const packetContentArray = [];
if (Array.isArray(packetContent)) {
packetContentArray.push(...packetContent)
}
else {
packetContentArray.push(packetContent)
}
for (let i = 0; i < packetContentArray.length; i++) {
const record = packetContentArray[i];
switch (record.className) {
case 'DocumentSerieUpdate': {
const doc = await MobileDocumentSerie.query().filter(`documentType eq '${record.documentType}' and userName eq '${event.eventArguments.userName}'`).executeSelect();
if (doc.length > 0) {
console.log(doc)
doc[0].value = record.current;
await MobileDocumentSerie.query().executeUpdate(doc[0]);
}
break;
}
case 'SalesOrderDelivery':{
const existingSalesOrders = await SalesOrderDelivery.query().filter('').executeSelect();
if(existingSalesOrders && existingSalesOrders.length>0){
existingSalesOrders[0].deliveryStatus = SalesOrderDelivery_deliveryStatus_enum.Delivered;
existingSalesOrders[0].actualDeliveryDate = new Date();
await SalesOrderDelivery.query().executeUpdate(existingSalesOrders[0]);
}
break;
}
}
}
}
default: return;
}
}
}

The example will only react on packets, which contain certain object types.

Every object with the packet array has the following special fields - e.g. these fields are always present:

  • appCode - the unique identifier of the application generated the packet

  • className - the type ( or class ) of the object, which corresponds to the business object

The code can inspect those fields and decide what to do with the content.