UNPKG

@universis/dining

Version:

Universis api for dining

216 lines (66 loc) 8.43 kB
"use strict";var _common = require("@themost/common"); var _data = require("@themost/data"); var _EnableAttachmentModel = _interopRequireDefault(require("./EnableAttachmentModel"));var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class;function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object.keys(descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object.defineProperty(target, property, desc);desc = null;}return desc;}let DiningRequestAction = (_dec = _data.EdmMapping.param('extraAttributes', 'Object', true, true), _dec2 = _data.EdmMapping.param('file', _data.EdmType.EdmStream, false), _dec3 = _data.EdmMapping.action('AddAttachment', 'Attachment'), _dec4 = _data.EdmMapping.param('attachment', 'Attachment', true, true), _dec5 = _data.EdmMapping.action('RemoveAttachment', 'Attachment'), _dec6 = _data.EdmMapping.param('attachment', 'Attachment', true, true), _dec7 = _data.EdmMapping.action('RejectAttachment', 'Attachment'), _dec8 = _data.EdmMapping.param('attachment', 'Attachment', true, true), _dec9 = _data.EdmMapping.action('RevertAttachment', 'Attachment'), _dec10 = _data.EdmMapping.param('item', 'DiningRequestActionMessage', true, true), _dec11 = _data.EdmMapping.action('messages', 'DiningRequestActionMessage'), _dec12 = _data.EdmMapping.func('review', 'DiningRequestActionReview'), _dec13 = _data.EdmMapping.param('item', 'DiningRequestActionReview', true, true), _dec14 = _data.EdmMapping.action('review', 'DiningRequestActionReview'), _dec15 = _data.EdmMapping.param('message', 'Object', true, true), _dec16 = _data.EdmMapping.param('attachment', _data.EdmType.EdmStream, true), _dec17 = _data.EdmMapping.action('SendMessage', 'DiningRequestActionMessage'), _dec18 = _data.EdmMapping.action('Cancel', 'DiningRequestAction'), (_class = class DiningRequestAction extends _EnableAttachmentModel.default {constructor() {super();} /** * Adds an attachment * @param {*} file * @param {*=} extraAttributes */async addAttachment(file, extraAttributes) {const attachment = Object.assign({ name: file.contentFileName, originalname: file.contentFileName }, file, extraAttributes);return await super.addAttachment(attachment);} /** * Removes an attachment * @param {*} attachment */async removeAttachment(attachment) {return await super.removeAttachment(attachment.id);} /** * Rejects an attachment * @param {*} attachment */async rejectAttachment(attachment) {return await super.rejectAttachment(attachment.id);} /** * Reverts (re-accepts) an attachment * @param {*} attachment */async revertAttachment(attachment) {return await super.revertAttachment(attachment.id);} /** * Add action messages * @param {Array<any>} items */async postMessages(item) {// set initiator item.action = this.getId();return this.context.model('DiningRequestActionMessage').save(item);} /** * Gets item review */getReview() {return this.context.model('DiningRequestActionReview').where('itemReviewed').equal(this.getId()).prepare();} /** * Set item review * @param {*} item */async setReview(item) {const DiningRequestActionReviews = this.context.model('DiningRequestActionReview'); // infer object state const currentReview = await DiningRequestActionReviews.where('itemReviewed').equal(this.getId()).getItem();if (currentReview == null) {if (item == null) {return;} // a new item is going to be inserted delete item.id; // set reviewed item item.itemReviewed = this.getId();} else {if (item == null) {// delete review DiningRequestActionReviews.remove(currentReview);} // up item.id = currentReview.id; // set reviewed item item.itemReviewed = this.getId();}return DiningRequestActionReviews.save(item);} /** * Adds an attachment * @param {{contentFileName:string}|*} attachment * @param {*=} message */sendMessage(attachment, message) {const thisArg = this;return new Promise((resolve, reject) => {this.context.db.executeInTransaction(cb => {(async () => {const DiningRequestActionMessage = thisArg.context.model('DiningRequestActionMessage');if (message.recipient) {var _await$thisArg$contex; // try to find the message recipient by bypassing privileges // note: Students do not-and should not-have read access to Account or User models. // this process will help to identify an account by its name, e.g. recipient: 'DiningUsers' const recipient = (_await$thisArg$contex = await thisArg.context.model('Account').where('name').equal(message.recipient.name || message.recipient).select('id').silent().value()) !== null && _await$thisArg$contex !== void 0 ? _await$thisArg$contex : await thisArg.context.model('Account').where('id').equal(message.recipient.id || message.recipient).select('id').silent().value();if (recipient == null) {throw new _common.DataError('The specified message recipient (User/Account) cannot be found.');} // and override the message recipient message.recipient = recipient;}const newMessage = DiningRequestActionMessage.convert(message);Object.assign(newMessage, { action: thisArg.getId() }); // add message await DiningRequestActionMessage.silent().save(newMessage); // if message has an attachment if (attachment) {// prepare attachment data const newAttachment = Object.assign({ name: attachment.contentFileName, originalname: attachment.contentFileName }, attachment); // and add attachment await newMessage.addAttachment(newAttachment);}})().then(() => {return cb();}).catch(err => {return cb(err);});}, err => {if (err) {return reject(err);}return resolve(message);});});} //remove dining request action async cancel() {/** * @type {import('@themost/data').DataModel} */const model = this.getModel();const item = await model.where('owner/name').equal(this.context.user.name).and('actionStatus/alternateName').equal('PotentialActionStatus').and('id').equal(this.getId()).getItem();if (item == null) {throw new _common.HttpForbiddenError();}return model.silent().remove(item);}}, (_applyDecoratedDescriptor(_class.prototype, "addAttachment", [_dec, _dec2, _dec3], Object.getOwnPropertyDescriptor(_class.prototype, "addAttachment"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "removeAttachment", [_dec4, _dec5], Object.getOwnPropertyDescriptor(_class.prototype, "removeAttachment"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "rejectAttachment", [_dec6, _dec7], Object.getOwnPropertyDescriptor(_class.prototype, "rejectAttachment"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "revertAttachment", [_dec8, _dec9], Object.getOwnPropertyDescriptor(_class.prototype, "revertAttachment"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "postMessages", [_dec10, _dec11], Object.getOwnPropertyDescriptor(_class.prototype, "postMessages"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getReview", [_dec12], Object.getOwnPropertyDescriptor(_class.prototype, "getReview"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "setReview", [_dec13, _dec14], Object.getOwnPropertyDescriptor(_class.prototype, "setReview"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "sendMessage", [_dec15, _dec16, _dec17], Object.getOwnPropertyDescriptor(_class.prototype, "sendMessage"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "cancel", [_dec18], Object.getOwnPropertyDescriptor(_class.prototype, "cancel"), _class.prototype)), _class));module.exports = DiningRequestAction; //# sourceMappingURL=DiningRequestAction.js.map