Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@
"in": "./lib/schemas/createPet.in.json",
"out": "./lib/schemas/createPet.out.json"
}
},
"testSelectModel": {
"main": "./lib/actions/testSelectModel.js",
"title": "Test Select Model",
"description": "Creates a pet and adds it to the shop",
"fields": {
"objectType": {
"viewClass": "SelectView",
"label": "Object Type",
"required": true,
"model": {
"user": "User",
"product": "Product"
},
"prompt": "Choose an Object Type"
},
"eventTypes": {
"viewClass": "MultiSelectView",
"label": "Event types",
"required": true,
"model": "getEventTypes",
"prompt": "Choose an Event Type"
}
},
"metadata": {
"in": "./lib/schemas/createPet.in.json",
"out": "./lib/schemas/createPet.out.json"
}
}
}
}
48 changes: 48 additions & 0 deletions lib/actions/testSelectModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { messages } = require('elasticio-node');

const eventTypesMap = {
user: {
'carts/create': 'Create',
'carts/update': 'Update',
},
product: {
'products/create': 'Create',
'products/update': 'Update',
'products/delete': 'Delete',
},
};

exports.process = async function process(msg, cfg) {
const { objectType, eventTypes } = cfg;
const { name } = msg.body;

const statuses = {
Available: 'available',
Pending: 'pending',
Sold: 'sold',
};

// map the status value from the readable value to the internal camel case one
const status = statuses[msg.body.status];

if (!name) {
throw new Error('Name is required');
}

if (!status) {
throw new Error('Status is required');
}

// create pet object to post
const pet = {
name,
status,
objectType,
eventTypes,
};
await this.emit('data', messages.newMessageWithBody(pet));
};

exports.getEventTypes = async function getEventTypes(cfg) {
return eventTypesMap[cfg.objectType];
};