UNPKG

@appsemble/node-utils

Version:

NodeJS utilities used by Appsemble internally.

45 lines 1.7 kB
import { assertKoaCondition } from '@appsemble/node-utils'; export function createDeleteAppResourceController(options) { return async (ctx) => { const { pathParams: { appId, resourceId, resourceType }, queryParams: { selectedGroupId }, user: authSubject, } = ctx; const { checkAppPermissions, deleteAppResource, getApp, getAppResource } = options; const app = await getApp({ context: ctx, query: { where: { id: appId } } }); const findOptions = { where: { id: resourceId, type: resourceType, AppId: appId, GroupId: selectedGroupId ?? null, expires: { or: [{ gt: new Date() }, { eq: null }] }, ...(app.demoMode ? { seed: false, ephemeral: true } : {}), }, }; const resource = await getAppResource({ app, id: resourceId, type: resourceType, context: ctx, findOptions, }); assertKoaCondition(resource != null, ctx, 404, 'Resource not found'); await checkAppPermissions({ context: ctx, permissions: [ resource.$author?.id === authSubject.id ? `$resource:${resourceType}:own:delete` : `$resource:${resourceType}:delete`, ], app, groupId: selectedGroupId, }); await deleteAppResource({ app, context: ctx, id: resourceId, type: resourceType, options, }); ctx.status = 204; }; } //# sourceMappingURL=createDeleteAppResourceController.js.map