@universis/dining
Version:
Universis api for dining
198 lines (182 loc) • 6.97 kB
JavaScript
import { DataError, HttpForbiddenError } from "@themost/common";
import { EdmMapping, EdmType } from "@themost/data";
import EnableAttachmentModel from './EnableAttachmentModel';
class DiningRequestAction extends EnableAttachmentModel {
constructor() {
super();
}
/**
* Adds an attachment
* @param {*} file
* @param {*=} extraAttributes
*/
.param('extraAttributes', 'Object', true, true)
.param('file', EdmType.EdmStream, false)
.action('AddAttachment', 'Attachment')
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
*/
.param('attachment', 'Attachment', true, true)
.action('RemoveAttachment', 'Attachment')
async removeAttachment(attachment) {
return await super.removeAttachment(attachment.id);
}
/**
* Rejects an attachment
* @param {*} attachment
*/
.param('attachment', 'Attachment', true, true)
.action('RejectAttachment', 'Attachment')
async rejectAttachment(attachment) {
return await super.rejectAttachment(attachment.id);
}
/**
* Reverts (re-accepts) an attachment
* @param {*} attachment
*/
.param('attachment', 'Attachment', true, true)
.action('RevertAttachment', 'Attachment')
async revertAttachment(attachment) {
return await super.revertAttachment(attachment.id);
}
/**
* Add action messages
* @param {Array<any>} items
*/
.param('item', 'DiningRequestActionMessage', true, true)
.action('messages', 'DiningRequestActionMessage')
async postMessages(item) {
// set initiator
item.action = this.getId();
return this.context.model('DiningRequestActionMessage').save(item);
}
/**
* Gets item review
*/
.func('review', 'DiningRequestActionReview')
getReview() {
return this.context.model('DiningRequestActionReview')
.where('itemReviewed').equal(this.getId()).prepare();
}
/**
* Set item review
* @param {*} item
*/
.param('item', 'DiningRequestActionReview', true, true)
.action('review', 'DiningRequestActionReview')
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
*/
.param('message', 'Object', true, true)
.param('attachment', EdmType.EdmStream, true)
.action('SendMessage', 'DiningRequestActionMessage')
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) {
// 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.context.model('Account')
.where('name').equal(message.recipient.name || message.recipient)
.select('id')
.silent()
.value()
?? await thisArg.context.model('Account')
.where('id').equal(message.recipient.id || message.recipient)
.select('id')
.silent()
.value()
if (recipient == null) {
throw new 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
.action('Cancel', 'DiningRequestAction')
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 HttpForbiddenError();
}
return model.silent().remove(item);
}
}
module.exports = DiningRequestAction;