Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let submissions = [];
const { FormSubmission } = context.models;
if (ids) {
submissions = await FormSubmission.find({ query: { id: { $in: args.ids } } });
} else {
if (form) {
submissions = await FormSubmission.find({ query: { "form.revision": form } });
} else if (parent) {
submissions = await FormSubmission.find({ query: { "form.parent": parent } });
}
}
// Take first submission, get parent Form entity, and get all distinct fields through all revisions.
if (submissions.length === 0) {
return new NotFoundResponse("No form submissions found.");
}
const parentForm = await submissions[0].form.parent;
const revisions = await parentForm.revisions;
const rows = [];
const fields = {};
// First extract all distinct fields across all form submissions.
for (let i = 0; i < revisions.length; i++) {
let revision = revisions[i];
for (let j = 0; j < revision.fields.length; j++) {
let field = revision.fields[j];
if (!fields[field.fieldId]) {
fields[field.fieldId] = field.label.value;
}
export default async (root: any, args: Object, context: Object) => {
if (!args.id && !args.parent && !args.slug) {
return new NotFoundResponse("Form ID or slug missing.");
}
// We utilize the same query used for listing published forms (single source of truth = less maintenance).
const listArgs = { ...args, perPage: 1 };
if (!listArgs.version) {
listArgs.sort = { version: -1 };
}
const plugin = getListPublishedFormsResolver(context);
const { forms, totalCount } = await plugin.resolve({ root, args: listArgs, context });
if (!Array.isArray(forms) || !Number.isInteger(totalCount)) {
throw Error(
`Resolver plugin "forms-resolver-list-published-forms" must return { forms: [Form], totalCount: Int }!`
);
export default async (root: any, args: Object, context: Object) => {
if (!args.parent && !args.url) {
return new NotFoundResponse("Page parent or URL missing.");
}
// We utilize the same query used for listing published pages (single source of truth = less maintenance).
const [page] = await listPublishedPages({ context, args: { ...args, perPage: 1 } });
if (!page) {
return new NotFoundResponse("The requested page was not found.");
}
return new Response(page);
};
export default async (root: any, args: Object, context: Object) => {
if (!args.parent && !args.url) {
return new NotFoundResponse("Page parent or URL missing.");
}
// We utilize the same query used for listing published pages (single source of truth = less maintenance).
const [page] = await listPublishedPages({ context, args: { ...args, perPage: 1 } });
if (!page) {
return new NotFoundResponse("The requested page was not found.");
}
return new Response(page);
};
export default async (root: any, args: Object, context: Object) => {
const { PbPage, PbSettings } = context.models;
const { id } = args;
const newHomePage = await PbPage.findById(id);
if (!newHomePage) {
return new NotFoundResponse(id);
}
const settings = await PbSettings.load();
if (settings.data.pages.home === newHomePage.parent) {
return new ErrorResponse({
code: "ALREADY_HOMEPAGE",
message: `The page is already set as homepage.`
});
}
if (!newHomePage.published) {
newHomePage.published = true;
await newHomePage.save();
}
settings.data.pages.home = newHomePage.parent;
}
const plugin = getListPublishedFormsResolver(context);
const { forms, totalCount } = await plugin.resolve({ root, args: listArgs, context });
if (!Array.isArray(forms) || !Number.isInteger(totalCount)) {
throw Error(
`Resolver plugin "forms-resolver-list-published-forms" must return { forms: [Form], totalCount: Int }!`
);
}
const [form] = forms;
if (!form) {
return new NotFoundResponse("The requested form was not found.");
}
return new Response(form);
};
export default async (root: any, args: Object, context: Object) => {
const { File } = context.models;
const { id } = args;
const file = await File.findById(id);
if (!file) {
return new NotFoundResponse(id ? `File "${id}" not found!` : "File not found!");
}
const s3 = new S3();
await s3
.deleteObject({
Bucket: S3_BUCKET,
Key: file.key
})
.promise();
return file
.delete()
.then(() => new Response(true))
.catch(
e =>
const notFound = (id?: string) => {
return new NotFoundResponse(id ? `Record "${id}" not found!` : "Record not found!");
};
export default async (root: any, args: Object, context: Object) => {
const { slug } = args;
const { PbMenu } = context.models;
const menu = await PbMenu.findOne({ query: { slug } });
if (!menu) {
return new NotFoundResponse("Menu not found.");
}
return new Response({
id: menu.id,
title: menu.title,
items: prepareMenuItems({ menu, context })
});
};
export default userFetcher => async (root: any, args: Object, context: Object) => {
const { id } = args;
const User = userFetcher(context);
const user = await User.findById(id);
if (!user) {
return new NotFoundResponse(id ? `User "${id}" not found!` : "User not found!");
}
try {
await user.delete();
const authPlugin = context.plugins
.byType("security-authentication-provider")
.filter(pl => pl.hasOwnProperty("deleteUser"))
.pop();
await authPlugin.deleteUser({ user }, context);
return new Response(true);
} catch (e) {
return new ErrorResponse({
code: e.code,