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) => {
        const models = Self.app.models;
        const targetTicketDms = await models.TicketDms.findById(id);
        const targetDms = await models.Dms.findById(targetTicketDms.dmsFk);
        const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}});

        await models.Dms.removeFile(ctx, targetTicketDms.dmsFk);
        await targetTicketDms.destroy();

        return targetDms.updateAttribute('dmsTypeFk', trashDmsType.id);
    };
};