diff --git a/deno.lock b/deno.lock index 73355fa..4df0ec0 100644 --- a/deno.lock +++ b/deno.lock @@ -1,27 +1,26 @@ { "version": "5", "specifiers": { - "jsr:@oak/commons@1": "1.0.0", - "jsr:@openai/openai@^4.98.0": "4.98.0", + "jsr:@oak/commons@1": "1.0.1", + "jsr:@openai/openai@^4.98.0": "4.102.0", "jsr:@std/assert@1": "1.0.13", - "jsr:@std/bytes@1": "1.0.5", - "jsr:@std/crypto@1": "1.0.4", + "jsr:@std/bytes@1": "1.0.6", + "jsr:@std/crypto@1": "1.0.5", "jsr:@std/encoding@1": "1.0.10", "jsr:@std/encoding@^1.0.10": "1.0.10", - "jsr:@std/http@1": "1.0.15", - "jsr:@std/internal@^1.0.6": "1.0.6", + "jsr:@std/http@1": "1.0.18", "jsr:@std/media-types@1": "1.1.0", - "jsr:@std/path@1": "1.0.9", - "npm:@types/node@*": "22.12.0", - "npm:mongodb@*": "6.1.0", + "jsr:@std/path@1": "1.1.0", + "npm:@types/node@*": "22.15.15", + "npm:mongodb@*": "6.17.0", "npm:mongodb@6.1.0": "6.1.0", "npm:path-to-regexp@^6.3.0": "6.3.0", - "npm:zod@*": "3.24.4", - "npm:zod@3": "3.24.4" + "npm:zod@*": "3.25.64", + "npm:zod@3": "3.25.64" }, "jsr": { - "@oak/commons@1.0.0": { - "integrity": "49805b55603c3627a9d6235c0655aa2b6222d3036b3a13ff0380c16368f607ac", + "@oak/commons@1.0.1": { + "integrity": "889ff210f0b4292591721be07244ecb1b5c118742f5273c70cf30d7cd4184d0c", "dependencies": [ "jsr:@std/assert", "jsr:@std/bytes", @@ -31,52 +30,46 @@ "jsr:@std/media-types" ] }, - "@openai/openai@4.98.0": { - "integrity": "c3f1b428c2529713f20d5f66b34eea9f9e2d3f5822f12a84ff561b4b80ecbc9f", + "@openai/openai@4.102.0": { + "integrity": "ea5201bc5c8a696d3a5139a9cd2252e3ab9b2b880769a9cd7f05602ea1a02acf", "dependencies": [ "npm:zod@3" ] }, "@std/assert@1.0.13": { - "integrity": "ae0d31e41919b12c656c742b22522c32fb26ed0cba32975cb0de2a273cb68b29", - "dependencies": [ - "jsr:@std/internal" - ] + "integrity": "ae0d31e41919b12c656c742b22522c32fb26ed0cba32975cb0de2a273cb68b29" }, - "@std/bytes@1.0.5": { - "integrity": "4465dd739d7963d964c809202ebea6d5c6b8e3829ef25c6a224290fbb8a1021e" + "@std/bytes@1.0.6": { + "integrity": "f6ac6adbd8ccd99314045f5703e23af0a68d7f7e58364b47d2c7f408aeb5820a" }, - "@std/crypto@1.0.4": { - "integrity": "cee245c453bd5366207f4d8aa25ea3e9c86cecad2be3fefcaa6cb17203d79340" + "@std/crypto@1.0.5": { + "integrity": "0dcfbb319fe0bba1bd3af904ceb4f948cde1b92979ec1614528380ed308a3b40" }, "@std/encoding@1.0.10": { "integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1" }, - "@std/http@1.0.15": { - "integrity": "435a4934b4e196e82a8233f724da525f7b7112f3566502f28815e94764c19159", + "@std/http@1.0.18": { + "integrity": "8d9546aa532c52a0cf318c74616db0638b4c1073405355d1b14f9e1591dccf20", "dependencies": [ "jsr:@std/encoding@^1.0.10" ] }, - "@std/internal@1.0.6": { - "integrity": "9533b128f230f73bd209408bb07a4b12f8d4255ab2a4d22a1fd6d87304aca9a4" - }, "@std/media-types@1.1.0": { "integrity": "c9d093f0c05c3512932b330e3cc1fe1d627b301db33a4c2c2185c02471d6eaa4" }, - "@std/path@1.0.9": { - "integrity": "260a49f11edd3db93dd38350bf9cd1b4d1366afa98e81b86167b4e3dd750129e" + "@std/path@1.1.0": { + "integrity": "ddc94f8e3c275627281cbc23341df6b8bcc874d70374f75fec2533521e3d6886" } }, "npm": { - "@mongodb-js/saslprep@1.2.2": { - "integrity": "sha512-EB0O3SCSNRUFk66iRCpI+cXzIjdswfCs7F6nOC3RAGJ7xr5YhaicvsRwJ9eyzYvYRlCSDUO/c7g4yNulxKC1WA==", + "@mongodb-js/saslprep@1.3.0": { + "integrity": "sha512-zlayKCsIjYb7/IdfqxorK5+xUMyi4vOKcFy10wKJYc63NSdKI8mNME+uJqfatkPmOSMMUiojrL58IePKBm3gvQ==", "dependencies": [ "sparse-bitfield" ] }, - "@types/node@22.12.0": { - "integrity": "sha512-Fll2FZ1riMjNmlmJOdAyY5pUbkftXslB5DgEzlIuNaiWhXd00FhWxVC/r4yV/4wBb9JfImTu+jiSvXTkJ7F/gA==", + "@types/node@22.15.15": { + "integrity": "sha512-R5muMcZob3/Jjchn5LcO8jdKwSCbzqmPB6ruBxMcf9kbxtniZHP327s6C37iOfuw8mbKK3cAQa7sEl7afLrQ8A==", "dependencies": [ "undici-types" ] @@ -84,6 +77,12 @@ "@types/webidl-conversions@7.0.3": { "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==" }, + "@types/whatwg-url@11.0.5": { + "integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==", + "dependencies": [ + "@types/webidl-conversions" + ] + }, "@types/whatwg-url@8.2.2": { "integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==", "dependencies": [ @@ -91,8 +90,8 @@ "@types/webidl-conversions" ] }, - "bson@6.10.3": { - "integrity": "sha512-MTxGsqgYTwfshYWTRdmZRC+M7FnG1b4y7RO7p2k3X24Wq0yv1m77Wsj0BzlPzd/IowgESfsruQCUToa7vbOpPQ==" + "bson@6.10.4": { + "integrity": "sha512-WIsKqkSC0ABoBJuT1LEX+2HEvNmNKKgnTAyd0fL8qzK4SH2i9NXg+t08YtdZp/V9IZ33cxe3iV4yM0qg8lMQng==" }, "memory-pager@1.5.0": { "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==" @@ -100,8 +99,15 @@ "mongodb-connection-string-url@2.6.0": { "integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==", "dependencies": [ - "@types/whatwg-url", - "whatwg-url" + "@types/whatwg-url@8.2.2", + "whatwg-url@11.0.0" + ] + }, + "mongodb-connection-string-url@3.0.2": { + "integrity": "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==", + "dependencies": [ + "@types/whatwg-url@11.0.5", + "whatwg-url@14.2.0" ] }, "mongodb@6.1.0": { @@ -109,15 +115,20 @@ "dependencies": [ "@mongodb-js/saslprep", "bson", - "mongodb-connection-string-url" + "mongodb-connection-string-url@2.6.0" + ] + }, + "mongodb@6.17.0": { + "integrity": "sha512-neerUzg/8U26cgruLysKEjJvoNSXhyID3RvzvdcpsIi2COYM3FS3o9nlH7fxFtefTb942dX3W9i37oPfCVj4wA==", + "dependencies": [ + "@mongodb-js/saslprep", + "bson", + "mongodb-connection-string-url@3.0.2" ] }, "path-to-regexp@6.3.0": { "integrity": "sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ==" }, - "pg-cloudflare@1.2.5": { - "integrity": "sha512-OOX22Vt0vOSRrdoUPKJ8Wi2OpE/o/h9T8X1s4qSkCedbNah9ei2W2765be8iMVxQUsvgT7zIAT2eIa9fs5+vtg==" - }, "punycode@2.3.1": { "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==" }, @@ -133,8 +144,14 @@ "punycode" ] }, - "undici-types@6.20.0": { - "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==" + "tr46@5.1.1": { + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "dependencies": [ + "punycode" + ] + }, + "undici-types@6.21.0": { + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==" }, "webidl-conversions@7.0.0": { "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==" @@ -142,19 +159,21 @@ "whatwg-url@11.0.0": { "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", "dependencies": [ - "tr46", + "tr46@3.0.0", + "webidl-conversions" + ] + }, + "whatwg-url@14.2.0": { + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "dependencies": [ + "tr46@5.1.1", "webidl-conversions" ] }, - "zod@3.24.4": { - "integrity": "sha512-OdqJE9UDRPwWsrHjLN2F8bPxvwJBK22EHLWtanu0LSYr5YqzsaaW3RMgmjwr8Rypg5k+meEJdSPXJZXE/yqOMg==" + "zod@3.25.64": { + "integrity": "sha512-hbP9FpSZf7pkS7hRVUrOjhwKJNyampPgtXKc3AN6DsWtoHsg2Sb4SQaS4Tcay380zSwd2VPo9G9180emBACp5g==" } }, - "redirects": { - "https://deno.land/x/bcrypt/mod.ts": "https://deno.land/x/bcrypt@v0.4.1/mod.ts", - "https://deno.land/x/fastest_levenshtein/mod.ts": "https://deno.land/x/fastest_levenshtein@1.0.10/mod.ts", - "https://deno.land/x/zod/mod.ts": "https://deno.land/x/zod@v3.24.4/mod.ts" - }, "remote": { "https://deno.land/std@0.221.0/encoding/_util.ts": "beacef316c1255da9bc8e95afb1fa56ed69baef919c88dc06ae6cb7a6103d376", "https://deno.land/std@0.221.0/encoding/base64.ts": "8ccae67a1227b875340a8582ff707f37b131df435b07080d3bb58e07f5f97807", @@ -169,11 +188,6 @@ "https://deno.land/x/bcrypt@v0.2.4/src/bcrypt/base64.ts": "b8266450a4f1eb6960f60f2f7986afc4dde6b45bd2d7ee7ba10789e67e17b9f7", "https://deno.land/x/bcrypt@v0.2.4/src/bcrypt/bcrypt.ts": "7845b01323458b530dcf6e9ab46e513c507b6ff1670457afaa0b3465721b56b4", "https://deno.land/x/bcrypt@v0.2.4/src/main.ts": "4ac3ccc2247ce729f6c7d57654909949b62aafdabd996483c97f34aa1afac82a", - "https://deno.land/x/bcrypt@v0.4.1/mod.ts": "ff09bdae282583cf5f7d87efe37ddcecef7f14f6d12e8b8066a3058db8c6c2f7", - "https://deno.land/x/bcrypt@v0.4.1/src/bcrypt/base64.ts": "b8266450a4f1eb6960f60f2f7986afc4dde6b45bd2d7ee7ba10789e67e17b9f7", - "https://deno.land/x/bcrypt@v0.4.1/src/bcrypt/bcrypt.ts": "ec221648cc6453ea5e3803bc817c01157dada06aa6f7a0ba6b9f87aae32b21e2", - "https://deno.land/x/bcrypt@v0.4.1/src/main.ts": "08d201b289c8d9c46f8839c69cd6625b213863db29775c7a200afc3b540e64f8", - "https://deno.land/x/bcrypt@v0.4.1/src/worker.ts": "5a73bdfee9c9e622f47c9733d374b627dce52fb3ec1e74c8226698b3fc57ffac", "https://deno.land/x/cors@v1.2.2/abcCors.ts": "cdf83a7eaa69a1bf3ab910d18b9422217902fac47601adcaf0afac5a61845d48", "https://deno.land/x/cors@v1.2.2/attainCors.ts": "7d6aba0f942495cc31119604e0895c9bb8edd8f8baa7fe78e6c655bd0b4cbf59", "https://deno.land/x/cors@v1.2.2/cors.ts": "0e2d9167e3685f9bcf48f565e312b6e1883fa458f7337e5ce7bc2e3b29767980", @@ -187,7 +201,6 @@ "https://deno.land/x/djwt@v3.0.2/mod.ts": "962d8f2c4d6a4db111f45d777b152356aec31ba7db0ca664601175a422629857", "https://deno.land/x/djwt@v3.0.2/signature.ts": "16238fbf558267c85dd6c0178045f006c8b914a7301db87149f3318326569272", "https://deno.land/x/djwt@v3.0.2/util.ts": "5cb264d2125c553678e11446bcfa0494025d120e3f59d0a3ab38f6800def697d", - "https://deno.land/x/fastest_levenshtein@1.0.10/mod.ts": "aea49d54b6bb37082b2377da2ea068331da07b2a515621d8eff97538b7157b40", "https://deno.land/x/oak@v17.1.4/application.ts": "69fb6462eed013562ee61239e60ea77e5df3abb2b0df34568593b9774d72e98f", "https://deno.land/x/oak@v17.1.4/body.ts": "f91d8e0298abbabe6acb543d1090e2a41aa665375918a575d72bd6b98b538e40", "https://deno.land/x/oak@v17.1.4/context.ts": "e04c3d67d68ee01a279aeded457b0d66255450593dacab22251e7eb0cf6a92e5", @@ -217,23 +230,13 @@ "https://deno.land/x/oak@v17.1.4/utils/resolve_path.ts": "aa39d54a003b38fee55f340a0cba3f93a7af85b8ddd5fbfb049a98fc0109b36d", "https://deno.land/x/oak@v17.1.4/utils/streams.ts": "c85ae197f5046d58d5a3e42221254a6c8e63172f9f01b77631e480e1dcc90e05", "https://deno.land/x/oak@v17.1.4/utils/type_guards.ts": "92ab7dae4bc5ce10206fb21ca13bedd13a4638848be15f9752146aedeab1fea4", - "https://deno.land/x/zod@v3.24.4/ZodError.ts": "27b41119736fcdc69cc72e63838bed1e9e1210c7ce721211f02256e06b443b55", - "https://deno.land/x/zod@v3.24.4/errors.ts": "5285922d2be9700cc0c70c95e4858952b07ae193aa0224be3cbd5cd5567eabef", - "https://deno.land/x/zod@v3.24.4/external.ts": "a6cfbd61e9e097d5f42f8a7ed6f92f93f51ff927d29c9fbaec04f03cbce130fe", - "https://deno.land/x/zod@v3.24.4/helpers/enumUtil.ts": "54efc393cc9860e687d8b81ff52e980def00fa67377ad0bf8b3104f8a5bf698c", - "https://deno.land/x/zod@v3.24.4/helpers/errorUtil.ts": "7a77328240be7b847af6de9189963bd9f79cab32bbc61502a9db4fe6683e2ea7", - "https://deno.land/x/zod@v3.24.4/helpers/parseUtil.ts": "c14814d167cc286972b6e094df88d7d982572a08424b7cd50f862036b6fcaa77", - "https://deno.land/x/zod@v3.24.4/helpers/partialUtil.ts": "998c2fe79795257d4d1cf10361e74492f3b7d852f61057c7c08ac0a46488b7e7", - "https://deno.land/x/zod@v3.24.4/helpers/typeAliases.ts": "0fda31a063c6736fc3cf9090dd94865c811dfff4f3cb8707b932bf937c6f2c3e", - "https://deno.land/x/zod@v3.24.4/helpers/util.ts": "baf6a3d8e0ac1887110a9ce8f73587d31cb2e5646e882a27bf2e774151a53d9b", - "https://deno.land/x/zod@v3.24.4/index.ts": "d27aabd973613985574bc31f39e45cb5d856aa122ef094a9f38a463b8ef1a268", - "https://deno.land/x/zod@v3.24.4/locales/en.ts": "a7a25cd23563ccb5e0eed214d9b31846305ddbcdb9c5c8f508b108943366ab4c", - "https://deno.land/x/zod@v3.24.4/mod.ts": "ec6e2b1255c1a350b80188f97bd0a6bac45801bb46fc48f50b9763aa66046039", - "https://deno.land/x/zod@v3.24.4/standard-schema.ts": "4abb2e7bd784fb95d219584673971bb317e74fb4fd0c74c196b558ba46df4456", - "https://deno.land/x/zod@v3.24.4/types.ts": "cfdd77407d4b06e79bd63e9cfdb6091489aeb5f432d03a68c0531c1965d001fb", - "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/deps.ts": "fdb5c487e374fb289d37836f0c89ce1f1d0a56c131e01c10cb797aec1e5a8f7f", + "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/deps.ts": "1cb6e03969134c62b5f0060918f39a422f871a67f6494ee5abe3f42140af8d5b", "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/models/event.model.ts": "ef793ba4fe8a964fbddbeb9b38440810db0ccbd257afbde9592ba336ec3eafc7", - "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/models/user.model.ts": "1e863d6b3803d775c5784e3dfd9d8374c379f46c5d66e64c5c442bd10effacd4" + "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/models/user.model.ts": "1e863d6b3803d775c5784e3dfd9d8374c379f46c5d66e64c5c442bd10effacd4", + "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/auth.service.ts": "15de25400f0c7118ff9b8076f143c8e33614585800b7325a5e34db405b919e24", + "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/events.service.ts": "58535b60580e22ea2adcaf27d06a40f2f9623477009b86629bdf0bba79bc5319", + "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/general.service.ts": "bf4a0afe2ad8f813fd62fefb536be0064894d8e930a1e673d497ab2852ff5682", + "https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/users.service.ts": "df8539de0c2bef507dadcd8e2410be30699a88bf4a54b7d3f11438c04367ce47" }, "workspace": { "dependencies": [ diff --git a/deps.ts b/deps.ts index 74ec303..f4e7d33 100644 --- a/deps.ts +++ b/deps.ts @@ -64,3 +64,26 @@ export type { FrequencyObject, FullEvent, } from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/models/event.model.ts'; + +export type { + ErrorResponse, + MessageResponse, +} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/general.service.ts'; + +export type { + LoginErrorDetails, + LoginErrorResponse, + LoginSuccessResponse, + MeSuccessResponse, + SignupErrorDetails, + SignupErrorResponse, + SignupSuccessResponse, +} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/auth.service.ts'; + +export type { + GetAllEventsErrorResponse, +} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/events.service.ts'; + +export type { + GetAllUsersSuccessResponse, +} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/users.service.ts'; diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index 53817e2..11fae62 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -1,7 +1,15 @@ // deno-lint-ignore-file require-await import { Context, + ErrorResponse, + LoginErrorDetails, + LoginErrorResponse, + LoginSuccessResponse, + MeSuccessResponse, RouterContext, + SignupErrorDetails, + SignupErrorResponse, + SignupSuccessResponse, Status, UserLogInSchema, UserSignUpSchema, @@ -13,13 +21,13 @@ export const getCurrentUser = async (ctx: Context) => { if (!user) { ctx.response.status = Status.Unauthorized; - ctx.response.body = { message: 'User not authenticated' }; + ctx.response.body = { error: 'User not authenticated' } as ErrorResponse; return; } ctx.response.body = { message: `Hello, ${user.username}!`, user, - }; + } as MeSuccessResponse; }; export const signUpUser = async (ctx: RouterContext<'/signup'>) => { @@ -32,20 +40,24 @@ export const signUpUser = async (ctx: RouterContext<'/signup'>) => { if (!userInput.success) { console.log('Validation Errors:', userInput.error); ctx.response.status = Status.BadRequest; - ctx.response.body = { errors: userInput.error.flatten() }; + ctx.response.body = { + errors: userInput.error.flatten() as SignupErrorDetails, + } as SignupErrorResponse; return; } try { const insertedId = await authService.createUser(userInput.data); ctx.response.status = Status.Created; - ctx.response.body = insertedId; + ctx.response.body = insertedId as SignupSuccessResponse; } catch (error) { ctx.response.status = Status.InternalServerError; if (error instanceof Error) { console.log('Sign up user error:', error); - ctx.response.body = { error: error.message }; + ctx.response.body = { error: error.message } as ErrorResponse; } else { - ctx.response.body = { error: 'Unkown error creating user' }; + ctx.response.body = { + error: 'Unkown error creating user', + } as ErrorResponse; } } }; @@ -55,22 +67,26 @@ export const loginUser = async (ctx: RouterContext<'/login'>) => { const userInput = UserLogInSchema.safeParse(body); if (!userInput.success) { ctx.response.status = Status.BadRequest; - ctx.response.body = { errors: userInput.error.flatten() }; + ctx.response.body = { + errors: userInput.error.flatten() as LoginErrorDetails, + } as LoginErrorResponse; console.error(userInput.error); return; } try { const token = await authService.logInUser(userInput.data); ctx.response.status = Status.OK; - ctx.response.body = { token }; + ctx.response.body = { token } as LoginSuccessResponse; } catch (error) { if (error instanceof Error) { ctx.response.status = Status.Unauthorized; - ctx.response.body = { error: error.message }; + ctx.response.body = { error: error.message } as ErrorResponse; console.error(error.message); } else { ctx.response.status = Status.InternalServerError; - ctx.response.body = { error: 'Unkown error creating token' }; + ctx.response.body = { + error: 'Unkown error creating token', + } as ErrorResponse; console.error(error); } } diff --git a/src/controllers/event.controller.ts b/src/controllers/event.controller.ts index 9d8deaf..0f6f5ed 100644 --- a/src/controllers/event.controller.ts +++ b/src/controllers/event.controller.ts @@ -1,13 +1,18 @@ import { Context, + ErrorResponse, eventFilterSchema, FullEvent, + GetAllEventsErrorResponse, + MessageResponse, ObjectId, RouterContext, Status, } from '../../deps.ts'; + import { eventService } from '../services/event.service.ts'; //import { generateEvents } from '../services/openai.service.ts'; + import { verifyToken } from '../utils/token.utils.ts'; import { Payload } from '../../deps.ts'; import { userService } from '../services/user.service.ts'; @@ -42,7 +47,7 @@ export const getAllEvents = async (ctx: Context) => { ctx.response.body = { error: 'Invalid query parameters', details: parseResult.error.format(), - }; + } as GetAllEventsErrorResponse; return; } @@ -69,7 +74,7 @@ export const getEventById = async (ctx: RouterContext<'/:id'>) => { if (!ObjectId.isValid(id)) { ctx.response.status = Status.BadRequest; - ctx.response.body = `Invalid event id "${id}"`; + ctx.response.body = { error: `Invalid event id "${id}"` } as ErrorResponse; return; } @@ -77,7 +82,9 @@ export const getEventById = async (ctx: RouterContext<'/:id'>) => { if (!event) { ctx.response.status = Status.NotFound; - ctx.response.body = `Event id "${id}" does not exist`; + ctx.response.body = { + error: `Event id "${id}" does not exist`, + } as ErrorResponse; return; } @@ -89,7 +96,7 @@ export const updateEventById = async (ctx: RouterContext<'/:id'>) => { if (!user) { ctx.response.status = Status.Unauthorized; - ctx.response.body = { message: 'User not authenticated' }; + ctx.response.body = { error: 'User not authenticated' } as ErrorResponse; return; } @@ -98,13 +105,15 @@ export const updateEventById = async (ctx: RouterContext<'/:id'>) => { console.log(event); if (!ObjectId.isValid(id)) { ctx.response.status = Status.BadRequest; - ctx.response.body = `Invalid event id "${id}"`; + ctx.response.body = { error: `Invalid event id "${id}"` } as ErrorResponse; return; } if (!eventService.isEvent(event)) { ctx.response.status = Status.BadRequest; - ctx.response.body = `Failed to validate event body`; + ctx.response.body = { + error: `Failed to validate event body`, + } as ErrorResponse; return; } @@ -112,11 +121,15 @@ export const updateEventById = async (ctx: RouterContext<'/:id'>) => { if (!result.acknowledged) { ctx.response.status = Status.NotFound; - ctx.response.body = `Failed to update event by id "${id}"`; + ctx.response.body = { + error: `Failed to update event by id "${id}"`, + } as ErrorResponse; return; } - ctx.response.body = { message: `Updated event id "${id}"` }; + ctx.response.body = { + message: `Updated event id "${id}"`, + } as MessageResponse; }; export const deleteEventById = async (ctx: RouterContext<'/:id'>) => { @@ -124,7 +137,7 @@ export const deleteEventById = async (ctx: RouterContext<'/:id'>) => { if (!user) { ctx.response.status = Status.Unauthorized; - ctx.response.body = { message: 'User not authenticated' }; + ctx.response.body = { error: 'User not authenticated' } as ErrorResponse; return; } @@ -132,7 +145,7 @@ export const deleteEventById = async (ctx: RouterContext<'/:id'>) => { if (!ObjectId.isValid(id)) { ctx.response.status = Status.BadRequest; - ctx.response.body = `Invalid event id "${id}"`; + ctx.response.body = { error: `Invalid event id "${id}"` } as ErrorResponse; return; } @@ -140,11 +153,15 @@ export const deleteEventById = async (ctx: RouterContext<'/:id'>) => { if (result.deletedCount == 0) { ctx.response.status = Status.NotFound; - ctx.response.body = `Could not find event id "${id}"`; + ctx.response.body = { + error: `Could not find event id "${id}"`, + } as ErrorResponse; return; } - ctx.response.body = { message: `Successfully deleted event id "${id}"` }; + ctx.response.body = { + message: `Successfully deleted event id "${id}"`, + } as MessageResponse; }; export const saveNewEvent = async (ctx: Context) => { @@ -153,9 +170,13 @@ export const saveNewEvent = async (ctx: Context) => { if (eventService.isEvent(event)) { eventService.saveEvents(event); - ctx.response.body = 'Event saved sucessfully'; + ctx.response.body = { + message: 'Event saved sucessfully', + } as MessageResponse; } else { - ctx.response.body = 'Create new event failed: Validation Error'; + ctx.response.body = { + error: 'Create new event failed: Validation Error', + } as ErrorResponse; } }; diff --git a/src/controllers/user.controller.ts b/src/controllers/user.controller.ts index a1248c4..b1c46c0 100644 --- a/src/controllers/user.controller.ts +++ b/src/controllers/user.controller.ts @@ -1,7 +1,12 @@ // deno-lint-ignore-file require-await import { Context, Payload, RouterContext, toSafeUser } from '../../deps.ts'; import { userService } from '../services/user.service.ts'; -import { Status } from '../../deps.ts'; +import { + ErrorResponse, + GetAllUsersSuccessResponse, + Status, +} from '../../deps.ts'; + export const getUserProfile = async (ctx: Context) => { // TODO: Get user data from ctx.state.user ctx.response.body = { message: 'Get user profile' }; @@ -23,14 +28,14 @@ export const getAllUsers = async (ctx: RouterContext<'/getUsers:role'>) => { ctx.response.status = Status.BadRequest; ctx.response.body = { error: 'Invalid role parameter. Use "user", "admin" or "all".', - }; + } as ErrorResponse; return; } try { const allUsers = await userService.getAllUsers(role); ctx.response.status = Status.OK; - ctx.response.body = allUsers.map(toSafeUser); + ctx.response.body = allUsers.map(toSafeUser) as GetAllUsersSuccessResponse; } catch (error: unknown) { if (error instanceof Error) { throw new Error(`Admin Error on getAllUsers: ${error.message}`);