@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
41 lines • 1.64 kB
JavaScript
import { generateResourceQuery } from '../../../../utils/resources.js';
export function createCountAppResourcesController(options) {
return async (ctx) => {
const { pathParams: { appId, resourceType }, queryParams: { $own, selectedGroupId }, user: authSubject, } = ctx;
const { checkAppPermissions, getApp, getAppResources } = options;
const app = await getApp({ context: ctx, query: { where: { id: appId } } });
await checkAppPermissions({
context: ctx,
permissions: [
$own ? `$resource:${resourceType}:own:query` : `$resource:${resourceType}:query`,
],
app,
groupId: selectedGroupId,
});
const { where } = generateResourceQuery(ctx, options);
const findOptions = {
attributes: ['id'],
where: {
and: [
where || {},
{
type: resourceType,
AppId: appId,
GroupId: selectedGroupId ?? null,
expires: { or: [{ gt: new Date() }, null] },
...(app.demoMode ? { seed: false, ephemeral: true } : {}),
...($own ? { AuthorId: authSubject?.id } : {}),
},
],
},
};
const resources = await getAppResources({
app,
findOptions,
type: resourceType,
context: ctx,
});
ctx.body = resources.length;
};
}
//# sourceMappingURL=createCountAppResourcesController.js.map