How to use the @webiny/api.NotFoundResponse function in @webiny/api

To help you get started, we’ve selected a few @webiny/api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formSubmissionResolvers / exportFormSubmissions.js View on Github external
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;
            }
github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formResolvers / getPublishedForm.js View on Github external
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 }!`
        );
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / pageResolvers / getPublishedPage.js View on Github external
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);
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / pageResolvers / getPublishedPage.js View on Github external
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);
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / pageResolvers / setHomePage.js View on Github external
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;
github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formResolvers / getPublishedForm.js View on Github external
}

    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);
};
github webiny / webiny-js / packages / api-files / src / plugins / resolvers / deleteFile.js View on Github external
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 =>
github webiny / webiny-js / packages / commodo-graphql / src / crudResolvers.js View on Github external
const notFound = (id?: string) => {
    return new NotFoundResponse(id ? `Record "${id}" not found!` : "Record not found!");
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / menuResolvers / getMenuBySlug.js View on Github external
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 })
    });
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / userResolvers / deleteUser.js View on Github external
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,