From 0b06c9fa6cae6f2b46a6546f9f2a44d1071dc090 Mon Sep 17 00:00:00 2001 From: Huilensolis Date: Mon, 24 Jun 2024 14:21:17 -0300 Subject: [PATCH 1/2] fix(api): not returning entry list if there are entries deleted --- apps/api/src/features/entry/router/index.ts | 275 ++++++++++---------- apps/api/turbo.json | 78 +++--- 2 files changed, 179 insertions(+), 174 deletions(-) diff --git a/apps/api/src/features/entry/router/index.ts b/apps/api/src/features/entry/router/index.ts index 6647891..9153d08 100644 --- a/apps/api/src/features/entry/router/index.ts +++ b/apps/api/src/features/entry/router/index.ts @@ -5,139 +5,144 @@ import { EntryInsertSchema, EntrySafeSchema } from "../models/entry.models"; import { EntryProvider } from "../providers"; export const EntryRoutes = new Elysia().group("/entry", (app) => - app - .use(isAuthenticated) - .post( - "/", - async ({ user, body, set }) => { - try { - const { data: newEntry, error } = await EntryProvider.createEntry({ - entry: body, - userId: user.id, - }); - - if (error || !newEntry) - throw new Error(error ?? "error creating entry"); - - set.status = "Created"; - return { id: newEntry.id }; - } catch (e) { - return error("Internal Server Error", {}); - } - }, - { - body: EntryInsertSchema, - response: { 201: t.Object({ id: t.String() }), 500: t.Object({}) }, - }, - ) - .get( - "/", - async ({ user, set }) => { - try { - const unsafeUserEntries = await EntryProvider.getEntriesListByUserId( - user.id, - ); - - const safeEntries = unsafeUserEntries.map((entry) => { - const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); - - if (!notDeletedEntry) { - throw new Error("entry is deleted"); - } - - const { safeEntry } = EntryAdapter.toSafeEntry(notDeletedEntry); - - return safeEntry; - }); - - set.status = "OK"; - return safeEntries; - } catch (e) { - return error("Internal Server Error", {}); - } - }, - { - response: { - 200: t.Array(EntrySafeSchema), - 500: t.Object({}), - }, - }, - ) - .get( - "/:id", - async ({ user, error, set, params }) => { - try { - const entry = await EntryProvider.getPrivateEntryById({ - entryId: params.id, - userId: user.id, - }); - - if (!entry) { - return error("Not Found", {}); - } - - const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); - - if (!notDeletedEntry) { - throw new Error("Entry Deleted"); - } - - const { safeEntry } = EntryAdapter.toSafeEntry(notDeletedEntry); - - set.status = "OK"; - return safeEntry; - } catch (e) { - return error("Not Found", {}); - } - }, - { - params: t.Object({ id: t.String() }), - response: { 200: EntrySafeSchema, 404: t.Object({}) }, - }, - ) - .delete( - "/:id", - async ({ params: { id: entryId }, set, user }) => { - try { - const { error } = await EntryProvider.deleteEntry({ - entryId, - userId: user.id, - }); - - if (error) throw new Error(error); - - set.status = "Accepted"; - return {}; - } catch (e) { - return error("Internal Server Error", {}); - } - }, - { params: t.Object({ id: t.String() }) }, - ) - .patch( - "/:id", - async ({ params: { id }, set, body }) => { - try { - const { error } = await EntryProvider.updateEntry({ - entryId: id, - values: body, - }); - - if (error) throw new Error(error); - - set.status = "No Content"; - return {}; - } catch (e) { - return error("Internal Server Error", {}); - } - }, - { - params: t.Object({ id: t.String() }), - body: EntryInsertSchema, - response: { - 201: t.Object({}), - 500: t.Object({}), - }, - }, - ), + app + .use(isAuthenticated) + .post( + "/", + async ({ user, body, set }) => { + try { + const { data: newEntry, error } = await EntryProvider.createEntry({ + entry: body, + userId: user.id, + }); + + if (error || !newEntry) + throw new Error(error ?? "error creating entry"); + + set.status = "Created"; + return { id: newEntry.id }; + } catch (e) { + return error("Internal Server Error", {}); + } + }, + { + body: EntryInsertSchema, + response: { 201: t.Object({ id: t.String() }), 500: t.Object({}) }, + }, + ) + .get( + "/", + async ({ user, set }) => { + try { + const unsafeUserEntries = await EntryProvider.getEntriesListByUserId( + user.id, + ); + + const notDeletedEntryList = unsafeUserEntries.filter((entry) => { + const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); + + if (!notDeletedEntry) { + return false; + } + + return true; + }); + + const safeEntries = notDeletedEntryList.map((entry) => { + const { safeEntry } = EntryAdapter.toSafeEntry(entry); + + return safeEntry; + }); + + set.status = "OK"; + return safeEntries; + } catch (e) { + console.log({ e }); + return error("Internal Server Error", {}); + } + }, + { + response: { + 200: t.Array(EntrySafeSchema), + 500: t.Object({}), + }, + }, + ) + .get( + "/:id", + async ({ user, error, set, params }) => { + try { + const entry = await EntryProvider.getPrivateEntryById({ + entryId: params.id, + userId: user.id, + }); + + if (!entry) { + return error("Not Found", {}); + } + + const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); + + if (!notDeletedEntry) { + throw new Error("Entry Deleted"); + } + + const { safeEntry } = EntryAdapter.toSafeEntry(notDeletedEntry); + + set.status = "OK"; + return safeEntry; + } catch (e) { + return error("Not Found", {}); + } + }, + { + params: t.Object({ id: t.String() }), + response: { 200: EntrySafeSchema, 404: t.Object({}) }, + }, + ) + .delete( + "/:id", + async ({ params: { id: entryId }, set, user }) => { + try { + const { error } = await EntryProvider.deleteEntry({ + entryId, + userId: user.id, + }); + + if (error) throw new Error(error); + + set.status = "Accepted"; + return {}; + } catch (e) { + return error("Internal Server Error", {}); + } + }, + { params: t.Object({ id: t.String() }) }, + ) + .patch( + "/:id", + async ({ params: { id }, set, body }) => { + try { + const { error } = await EntryProvider.updateEntry({ + entryId: id, + values: body, + }); + + if (error) throw new Error(error); + + set.status = "No Content"; + return {}; + } catch (e) { + return error("Internal Server Error", {}); + } + }, + { + params: t.Object({ id: t.String() }), + body: EntryInsertSchema, + response: { + 201: t.Object({}), + 500: t.Object({}), + }, + }, + ), ); diff --git a/apps/api/turbo.json b/apps/api/turbo.json index b7b9de6..a5078fb 100644 --- a/apps/api/turbo.json +++ b/apps/api/turbo.json @@ -1,41 +1,41 @@ { - "extends": ["//"], - "tasks": { - "build": { - "outputs": ["./dist/**"], - "cache": true, - "dependsOn": ["setup"] - }, - "dev": { - "dependsOn": ["setup"], - "cache": true, - "inputs": ["./src/**", "!./tests/**"] - }, - "setup": { - "cache": false, - "inputs": ["scripts/**", "compose.yaml", "drizzle.config.ts", ".env"] - }, - "clean-up": { - "cache": false - }, - "test": { - "cache": true, - "inputs": ["tests/**", ".env", "!./src/**"], - "dependsOn": ["setup"] - }, - "start": { - "cache": true, - "inputs": ["./src/**", "!./tests/**", ".env"], - "dependsOn": ["build"] - }, - "generate-migration": { - "cache": false - }, - "migrate": { - "dependsOn": ["generate-migration"] - }, - "lint": {}, - "format": {}, - "check": {} - } + "extends": ["//"], + "tasks": { + "build": { + "outputs": ["./dist/**"], + "cache": true, + "dependsOn": ["setup"] + }, + "dev": { + "dependsOn": ["setup"], + "cache": false, + "inputs": ["./src/**", "!./tests/**"] + }, + "setup": { + "cache": false, + "inputs": ["scripts/**", "compose.yaml", "drizzle.config.ts", ".env"] + }, + "clean-up": { + "cache": false + }, + "test": { + "cache": true, + "inputs": ["tests/**", ".env", "!./src/**"], + "dependsOn": ["setup"] + }, + "start": { + "cache": true, + "inputs": ["./src/**", "!./tests/**", ".env"], + "dependsOn": ["build"] + }, + "generate-migration": { + "cache": false + }, + "migrate": { + "dependsOn": ["generate-migration"] + }, + "lint": {}, + "format": {}, + "check": {} + } } From afc35a736488198341457030df5b02656568b6ae Mon Sep 17 00:00:00 2001 From: Github Action Date: Mon, 24 Jun 2024 17:22:02 +0000 Subject: [PATCH 2/2] format(api): formatt code --- apps/api/src/features/entry/router/index.ts | 280 +++++++++--------- .../api/tests/integration/entry/patch.test.ts | 56 ++-- apps/api/turbo.json | 78 ++--- 3 files changed, 207 insertions(+), 207 deletions(-) diff --git a/apps/api/src/features/entry/router/index.ts b/apps/api/src/features/entry/router/index.ts index 9153d08..89244fe 100644 --- a/apps/api/src/features/entry/router/index.ts +++ b/apps/api/src/features/entry/router/index.ts @@ -5,144 +5,144 @@ import { EntryInsertSchema, EntrySafeSchema } from "../models/entry.models"; import { EntryProvider } from "../providers"; export const EntryRoutes = new Elysia().group("/entry", (app) => - app - .use(isAuthenticated) - .post( - "/", - async ({ user, body, set }) => { - try { - const { data: newEntry, error } = await EntryProvider.createEntry({ - entry: body, - userId: user.id, - }); - - if (error || !newEntry) - throw new Error(error ?? "error creating entry"); - - set.status = "Created"; - return { id: newEntry.id }; - } catch (e) { - return error("Internal Server Error", {}); - } - }, - { - body: EntryInsertSchema, - response: { 201: t.Object({ id: t.String() }), 500: t.Object({}) }, - }, - ) - .get( - "/", - async ({ user, set }) => { - try { - const unsafeUserEntries = await EntryProvider.getEntriesListByUserId( - user.id, - ); - - const notDeletedEntryList = unsafeUserEntries.filter((entry) => { - const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); - - if (!notDeletedEntry) { - return false; - } - - return true; - }); - - const safeEntries = notDeletedEntryList.map((entry) => { - const { safeEntry } = EntryAdapter.toSafeEntry(entry); - - return safeEntry; - }); - - set.status = "OK"; - return safeEntries; - } catch (e) { - console.log({ e }); - return error("Internal Server Error", {}); - } - }, - { - response: { - 200: t.Array(EntrySafeSchema), - 500: t.Object({}), - }, - }, - ) - .get( - "/:id", - async ({ user, error, set, params }) => { - try { - const entry = await EntryProvider.getPrivateEntryById({ - entryId: params.id, - userId: user.id, - }); - - if (!entry) { - return error("Not Found", {}); - } - - const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); - - if (!notDeletedEntry) { - throw new Error("Entry Deleted"); - } - - const { safeEntry } = EntryAdapter.toSafeEntry(notDeletedEntry); - - set.status = "OK"; - return safeEntry; - } catch (e) { - return error("Not Found", {}); - } - }, - { - params: t.Object({ id: t.String() }), - response: { 200: EntrySafeSchema, 404: t.Object({}) }, - }, - ) - .delete( - "/:id", - async ({ params: { id: entryId }, set, user }) => { - try { - const { error } = await EntryProvider.deleteEntry({ - entryId, - userId: user.id, - }); - - if (error) throw new Error(error); - - set.status = "Accepted"; - return {}; - } catch (e) { - return error("Internal Server Error", {}); - } - }, - { params: t.Object({ id: t.String() }) }, - ) - .patch( - "/:id", - async ({ params: { id }, set, body }) => { - try { - const { error } = await EntryProvider.updateEntry({ - entryId: id, - values: body, - }); - - if (error) throw new Error(error); - - set.status = "No Content"; - return {}; - } catch (e) { - return error("Internal Server Error", {}); - } - }, - { - params: t.Object({ id: t.String() }), - body: EntryInsertSchema, - response: { - 201: t.Object({}), - 500: t.Object({}), - }, - }, - ), + app + .use(isAuthenticated) + .post( + "/", + async ({ user, body, set }) => { + try { + const { data: newEntry, error } = await EntryProvider.createEntry({ + entry: body, + userId: user.id, + }); + + if (error || !newEntry) + throw new Error(error ?? "error creating entry"); + + set.status = "Created"; + return { id: newEntry.id }; + } catch (e) { + return error("Internal Server Error", {}); + } + }, + { + body: EntryInsertSchema, + response: { 201: t.Object({ id: t.String() }), 500: t.Object({}) }, + }, + ) + .get( + "/", + async ({ user, set }) => { + try { + const unsafeUserEntries = await EntryProvider.getEntriesListByUserId( + user.id, + ); + + const notDeletedEntryList = unsafeUserEntries.filter((entry) => { + const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); + + if (!notDeletedEntry) { + return false; + } + + return true; + }); + + const safeEntries = notDeletedEntryList.map((entry) => { + const { safeEntry } = EntryAdapter.toSafeEntry(entry); + + return safeEntry; + }); + + set.status = "OK"; + return safeEntries; + } catch (e) { + console.log({ e }); + return error("Internal Server Error", {}); + } + }, + { + response: { + 200: t.Array(EntrySafeSchema), + 500: t.Object({}), + }, + }, + ) + .get( + "/:id", + async ({ user, error, set, params }) => { + try { + const entry = await EntryProvider.getPrivateEntryById({ + entryId: params.id, + userId: user.id, + }); + + if (!entry) { + return error("Not Found", {}); + } + + const { entry: notDeletedEntry } = EntryAdapter.toNotDeleted(entry); + + if (!notDeletedEntry) { + throw new Error("Entry Deleted"); + } + + const { safeEntry } = EntryAdapter.toSafeEntry(notDeletedEntry); + + set.status = "OK"; + return safeEntry; + } catch (e) { + return error("Not Found", {}); + } + }, + { + params: t.Object({ id: t.String() }), + response: { 200: EntrySafeSchema, 404: t.Object({}) }, + }, + ) + .delete( + "/:id", + async ({ params: { id: entryId }, set, user }) => { + try { + const { error } = await EntryProvider.deleteEntry({ + entryId, + userId: user.id, + }); + + if (error) throw new Error(error); + + set.status = "Accepted"; + return {}; + } catch (e) { + return error("Internal Server Error", {}); + } + }, + { params: t.Object({ id: t.String() }) }, + ) + .patch( + "/:id", + async ({ params: { id }, set, body }) => { + try { + const { error } = await EntryProvider.updateEntry({ + entryId: id, + values: body, + }); + + if (error) throw new Error(error); + + set.status = "No Content"; + return {}; + } catch (e) { + return error("Internal Server Error", {}); + } + }, + { + params: t.Object({ id: t.String() }), + body: EntryInsertSchema, + response: { + 201: t.Object({}), + 500: t.Object({}), + }, + }, + ), ); diff --git a/apps/api/tests/integration/entry/patch.test.ts b/apps/api/tests/integration/entry/patch.test.ts index e95675d..24d0aaf 100644 --- a/apps/api/tests/integration/entry/patch.test.ts +++ b/apps/api/tests/integration/entry/patch.test.ts @@ -6,37 +6,37 @@ import { createUser } from "@/tests/lib/user"; import { endpointPath } from "."; describe("Test PATCH method on entries endpoints", () => { - describe("Succesfull request", async () => { - const { cookie } = await createUser({}); + describe("Succesfull request", async () => { + const { cookie } = await createUser({}); - const { EntryId } = await createNewEntry( - { title: "untitled", content: EXAMPLE_DOCUMENT_CONTENT, word_count: 0 }, - cookie, - ); + const { EntryId } = await createNewEntry( + { title: "untitled", content: EXAMPLE_DOCUMENT_CONTENT, word_count: 0 }, + cookie, + ); - const res = await app.handle( - new Request(`${endpointPath}/${EntryId}`, { - method: "PATCH", - headers: { - "Content-Type": "application/json; charset=utf-8", - cookie: cookie, - }, - body: JSON.stringify({ - title: "test", - content: EXAMPLE_DOCUMENT_CONTENT, - word_count: 291, - }), - }), - ); + const res = await app.handle( + new Request(`${endpointPath}/${EntryId}`, { + method: "PATCH", + headers: { + "Content-Type": "application/json; charset=utf-8", + cookie: cookie, + }, + body: JSON.stringify({ + title: "test", + content: EXAMPLE_DOCUMENT_CONTENT, + word_count: 291, + }), + }), + ); - it("Should return status code 204", () => { - expect(res.status).toBe(204); - }); + it("Should return status code 204", () => { + expect(res.status).toBe(204); + }); - it("Should return empty object on body", async () => { - const body = await res.json(); + it("Should return empty object on body", async () => { + const body = await res.json(); - expect(body).toBeEmptyObject(); - }); - }); + expect(body).toBeEmptyObject(); + }); + }); }); diff --git a/apps/api/turbo.json b/apps/api/turbo.json index a5078fb..7efcfc7 100644 --- a/apps/api/turbo.json +++ b/apps/api/turbo.json @@ -1,41 +1,41 @@ { - "extends": ["//"], - "tasks": { - "build": { - "outputs": ["./dist/**"], - "cache": true, - "dependsOn": ["setup"] - }, - "dev": { - "dependsOn": ["setup"], - "cache": false, - "inputs": ["./src/**", "!./tests/**"] - }, - "setup": { - "cache": false, - "inputs": ["scripts/**", "compose.yaml", "drizzle.config.ts", ".env"] - }, - "clean-up": { - "cache": false - }, - "test": { - "cache": true, - "inputs": ["tests/**", ".env", "!./src/**"], - "dependsOn": ["setup"] - }, - "start": { - "cache": true, - "inputs": ["./src/**", "!./tests/**", ".env"], - "dependsOn": ["build"] - }, - "generate-migration": { - "cache": false - }, - "migrate": { - "dependsOn": ["generate-migration"] - }, - "lint": {}, - "format": {}, - "check": {} - } + "extends": ["//"], + "tasks": { + "build": { + "outputs": ["./dist/**"], + "cache": true, + "dependsOn": ["setup"] + }, + "dev": { + "dependsOn": ["setup"], + "cache": false, + "inputs": ["./src/**", "!./tests/**"] + }, + "setup": { + "cache": false, + "inputs": ["scripts/**", "compose.yaml", "drizzle.config.ts", ".env"] + }, + "clean-up": { + "cache": false + }, + "test": { + "cache": true, + "inputs": ["tests/**", ".env", "!./src/**"], + "dependsOn": ["setup"] + }, + "start": { + "cache": true, + "inputs": ["./src/**", "!./tests/**", ".env"], + "dependsOn": ["build"] + }, + "generate-migration": { + "cache": false + }, + "migrate": { + "dependsOn": ["generate-migration"] + }, + "lint": {}, + "format": {}, + "check": {} + } }