module.exports = Self => { Self.remoteMethodCtx('removeFile', { description: 'Removes a ticket document', accessType: 'WRITE', accepts: { arg: 'id', type: 'number', description: 'The document id', http: {source: 'path'} }, returns: { type: 'object', root: true }, http: { path: `/:id/removeFile`, verb: 'POST' } }); Self.removeFile = async(ctx, id, options) => { const models = Self.app.models; const myOptions = {}; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const targetTicketDms = await models.TicketDms.findById(id, null, myOptions); const targetDms = await models.Dms.findById(targetTicketDms.dmsFk, null, myOptions); const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions); await models.Dms.removeFile(ctx, targetTicketDms.dmsFk, myOptions); await targetTicketDms.destroy(myOptions); await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions); if (tx) await tx.commit(); return targetDms; } catch (e) { if (tx) await tx.rollback(); throw e; } }; };