From 6b28e3742fe935ec83e7679e52caaaede6c9dc0f Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Tue, 6 Feb 2024 13:10:44 -0500 Subject: [PATCH] Reorganize Mirage draft endpoints (#581) Reorganize draft endpoints --- web/mirage/config.ts | 282 +++++++++++++++++++++---------------------- 1 file changed, 137 insertions(+), 145 deletions(-) diff --git a/web/mirage/config.ts b/web/mirage/config.ts index 1d88dcdf1..869b3969e 100644 --- a/web/mirage/config.ts +++ b/web/mirage/config.ts @@ -435,6 +435,143 @@ export default function (mirageConfig) { } }); + /************************************************************************* + * + * Draft requests + * + *************************************************************************/ + + // Create a new draft + this.post("/drafts", (schema, request) => { + const document = schema.document.create({ + ...JSON.parse(request.requestBody), + }); + + document.update({ + objectID: document.id, + owners: [TEST_USER_EMAIL], + appCreated: true, + status: "WIP", + }); + + return new Response(200, {}, document.attrs); + }); + + // Return all the user's drafts + this.get("/drafts", (schema, request) => { + const params = request.queryParams; + const { facetFilters } = params; + const allDocs = this.schema.document.all().models; + const drafts = allDocs.filter((doc) => { + if (facetFilters.includes(`owners:${TEST_USER_EMAIL}`)) { + return ( + doc.attrs.isDraft && doc.attrs.owners.includes(TEST_USER_EMAIL) + ); + } else { + return doc.attrs.isDraft; + } + }); + + return new Response( + 200, + {}, + { + facets: [], + Hits: drafts, + params: "", + page: 0, + }, + ); + }); + + // Return a draft by ID + this.get("/drafts/:document_id", (schema, request) => { + return new Response( + 200, + {}, + schema.document.findBy({ + objectID: request.params.document_id, + }).attrs, + ); + }); + + // Determine if a draft is shareable + this.get("/drafts/:document_id/shareable", () => { + return new Response(200, {}, { isShareable: false }); + }); + + // Update whether a draft is shareable. + this.put("/drafts/:document_id/shareable", (schema, request) => { + const isShareable = JSON.parse(request.requestBody).isShareable; + + let doc = schema.document.findBy({ + objectID: request.params.document_id, + }); + + if (doc) { + doc.update({ isShareable }); + return new Response(200, {}, doc.attrs); + } + }); + + // Fetch a draft's related resources + this.get("drafts/:document_id/related-resources", (schema, request) => { + let hermesDocuments = schema.relatedHermesDocument + .all() + .models.map((doc) => { + return doc.attrs; + }); + let externalLinks = schema.relatedExternalLinks + .all() + .models.map((link) => { + return link.attrs; + }); + + return new Response(200, {}, { hermesDocuments, externalLinks }); + }); + + // Save a draft's related resources + this.put("/drafts/:document_id/related-resources", (schema, request) => { + let requestBody = JSON.parse(request.requestBody); + let { hermesDocuments, externalLinks } = requestBody; + + let doc = schema.document.findBy({ + objectID: request.params.document_id, + }); + + if (doc) { + doc.update({ + hermesDocuments, + externalLinks, + }); + return new Response(200, {}, doc.attrs); + } + }); + + // Save a draft + this.patch("/drafts/:document_id", (schema, request) => { + let document = schema.document.findBy({ + objectID: request.params.document_id, + }); + if (document) { + let attrs = JSON.parse(request.requestBody); + + if ("customFields" in attrs) { + attrs.customFields.forEach((field) => { + document.attrs[field.name] = field.value; + }); + } + + if ("product" in attrs) { + attrs.docNumber = getTestDocNumber(attrs.product); + } + + document.update(attrs); + + return new Response(200, {}, document.attrs); + } + }); + /************************************************************************* * * HEAD requests @@ -479,24 +616,6 @@ export default function (mirageConfig) { return new Response(200, {}, matches.models); }); - /** - * Called when a user creates a new document. - */ - this.post("/drafts", (schema, request) => { - const document = schema.document.create({ - ...JSON.parse(request.requestBody), - }); - - document.update({ - objectID: document.id, - owners: [TEST_USER_EMAIL], - appCreated: true, - status: "WIP", - }); - - return new Response(200, {}, document.attrs); - }); - /** * Used when publishing a draft for review. * Updates the document's status and isDraft properties. @@ -668,44 +787,6 @@ export default function (mirageConfig) { ); }); - /** - * Used by the Document route to get a document's draft. - */ - this.get("/drafts/:document_id", (schema, request) => { - return new Response( - 200, - {}, - schema.document.findBy({ - objectID: request.params.document_id, - }).attrs, - ); - }); - - /** - * Used by the document sidebar to determine if a draft `isShareable`. - */ - this.get("/documents/:document_id/shareable", () => { - return new Response(200, {}, { isShareable: false }); - }); - - /** - * Used by the RelatedResources component when the doc is a draft. - */ - this.get("drafts/:document_id/related-resources", (schema, request) => { - let hermesDocuments = schema.relatedHermesDocument - .all() - .models.map((doc) => { - return doc.attrs; - }); - let externalLinks = schema.relatedExternalLinks - .all() - .models.map((link) => { - return link.attrs; - }); - - return new Response(200, {}, { hermesDocuments, externalLinks }); - }); - /** * Used by the RelatedResources component when the doc is published. */ @@ -727,36 +808,6 @@ export default function (mirageConfig) { }, ); - /** - * Used by the /drafts route's getDraftResults method to fetch - * a list of facets and draft results. - */ - this.get("/drafts", (schema, request) => { - const params = request.queryParams; - const { facetFilters } = params; - const allDocs = this.schema.document.all().models; - const drafts = allDocs.filter((doc) => { - if (facetFilters.includes(`owners:${TEST_USER_EMAIL}`)) { - return ( - doc.attrs.isDraft && doc.attrs.owners.includes(TEST_USER_EMAIL) - ); - } else { - return doc.attrs.isDraft; - } - }); - - return new Response( - 200, - {}, - { - facets: [], - Hits: drafts, - params: "", - page: 0, - }, - ); - }); - /** * Used by the Dashboard route to get a user's recently viewed documents. */ @@ -861,32 +912,6 @@ export default function (mirageConfig) { }, ); - /** - * Used by the sidebar to save document properties, e.g., productArea. - */ - this.patch("/drafts/:document_id", (schema, request) => { - let document = schema.document.findBy({ - objectID: request.params.document_id, - }); - if (document) { - let attrs = JSON.parse(request.requestBody); - - if ("customFields" in attrs) { - attrs.customFields.forEach((field) => { - document.attrs[field.name] = field.value; - }); - } - - if ("product" in attrs) { - attrs.docNumber = getTestDocNumber(attrs.product); - } - - document.update(attrs); - - return new Response(200, {}, document.attrs); - } - }); - /** * Used by the sidebar to update a document, * e.g., to change a its status. @@ -908,25 +933,6 @@ export default function (mirageConfig) { * *************************************************************************/ - // Related resources (drafts) - - this.put("/drafts/:document_id/related-resources", (schema, request) => { - let requestBody = JSON.parse(request.requestBody); - let { hermesDocuments, externalLinks } = requestBody; - - let doc = schema.document.findBy({ - objectID: request.params.document_id, - }); - - if (doc) { - doc.update({ - hermesDocuments, - externalLinks, - }); - return new Response(200, {}, doc.attrs); - } - }); - // Related resources (published docs) this.put( @@ -983,20 +989,6 @@ export default function (mirageConfig) { return new Response(200, {}, project.attrs); } }); - - // Update whether a draft is shareable. - this.put("/drafts/:document_id/shareable", (schema, request) => { - const isShareable = JSON.parse(request.requestBody).isShareable; - - let doc = schema.document.findBy({ - objectID: request.params.document_id, - }); - - if (doc) { - doc.update({ isShareable }); - return new Response(200, {}, doc.attrs); - } - }); }, };