UNPKG

@appsemble/node-utils

Version:

NodeJS utilities used by Appsemble internally.

47 lines 2.08 kB
import { assertKoaCondition, getRemapperContext, getResourceDefinition, } from '@appsemble/node-utils'; import { defaultLocale, remap } from '@appsemble/utils'; export function createGetAppResourceByIdController(options) { return async (ctx) => { const { pathParams: { appId, resourceId, resourceType }, queryParams: { selectedGroupId, view }, user: authSubject, } = ctx; const { checkAppPermissions, 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() }, 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:get` : view ? `$resource:${resourceType}:get:${view}` : `$resource:${resourceType}:get`, ], app, groupId: selectedGroupId, }); if (view) { const context = await getRemapperContext(app, app.definition.defaultLanguage || defaultLocale, options, ctx); const resourceDefinition = getResourceDefinition(app.definition, resourceType, ctx, view); ctx.body = remap(resourceDefinition.views?.[view].remap ?? null, resource, context); return; } ctx.body = resource; }; } //# sourceMappingURL=createGetAppResourceByIdController.js.map