Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor schemas #143

Merged
merged 23 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"prettier": "3.0.3",
"semantic-release": "19.0.2",
"semantic-release-slack-bot": "3.5.2",
"typescript": "4.9.4"
"typescript": "5.0.2"
},
"prettier": {
"printWidth": 80,
Expand Down
6 changes: 4 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"author": "MNA",
"license": "MIT",
"main": "src/index.js",
"types": "src/index.ts",
"scripts": {
"start": "node --enable-source-maps dist/index.js",
"dev": "tsup src/index.ts --watch . --watch ../shared --onSuccess 'yarn run copyPublic && yarn run start'",
Expand All @@ -23,6 +23,7 @@
"@fastify/swagger-ui": "^1.6.0",
"@fastify/type-provider-typebox": "^3.5.0",
"@hapi/boom": "^10.0.1",
"@http-wizard/core": "^1.3.16",
"@json2csv/node": "^6.1.3",
"@json2csv/plainjs": "^6.1.3",
"@slack/bolt": "^3.14.0",
Expand All @@ -35,6 +36,7 @@
"env-var": "7.1.1",
"fastify": "^4.23.2",
"fastify-qs": "^4.0.1",
"fastify-type-provider-zod": "^1.1.9",
"jsonwebtoken": "8.5.1",
"kysely": "0.26.0",
"lodash": "^4.17.21",
Expand Down Expand Up @@ -77,6 +79,6 @@
"jest": "^29.5.0",
"kysely-codegen": "^0.10.0",
"ts-jest": "^29.1.0",
"typescript": "4.9.4"
"typescript": "5.0.2"
}
}
20 changes: 10 additions & 10 deletions server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import { z } from "zod";
import { basepath } from "./basepath";
import { migrateDownDB, migrateToLatest } from "./migrations/migrate";
import { createUser } from "./modules/core/usecases/createUser/createUser.usecase";
import { importDataEtablissements } from "./modules/data/usecases/importDataEtablissements/importDataEtablissements.usecase";
import { importDataFormations } from "./modules/data/usecases/importDataFormations/importDataFormations.usecase";
import { importDispositifs } from "./modules/data/usecases/importDispositifs/importDispositifs.usecase";
import { importFamillesMetiers } from "./modules/data/usecases/importFamillesMetiers/importFamillesMetiers.usecase";
import { importFormations } from "./modules/data/usecases/importFormationEtablissement/importFormationEtablissements.usecase";
import { importIndicateursAcademie } from "./modules/data/usecases/importIndicateursAcademie/importIndicateursAcademie.usecase";
import { importIndicateursRegion } from "./modules/data/usecases/importIndicateursRegion/importIndicateursRegion.usecase";
import { importNiveauxDiplome } from "./modules/data/usecases/importNiveauxDiplome/importNiveauxDiplome.usecase";
import { importRawFile } from "./modules/data/usecases/importRawFile/importRawFile.usecase";
import { importLieuxGeographiques } from "./modules/data/usecases/importRegions/importLieuxGeographiques.usecase";
import { importDataEtablissements } from "./modules/import/usecases/importDataEtablissements/importDataEtablissements.usecase";
import { importDataFormations } from "./modules/import/usecases/importDataFormations/importDataFormations.usecase";
import { importDispositifs } from "./modules/import/usecases/importDispositifs/importDispositifs.usecase";
import { importFamillesMetiers } from "./modules/import/usecases/importFamillesMetiers/importFamillesMetiers.usecase";
import { importFormations } from "./modules/import/usecases/importFormationEtablissement/importFormationEtablissements.usecase";
import { importIndicateursAcademie } from "./modules/import/usecases/importIndicateursAcademie/importIndicateursAcademie.usecase";
import { importIndicateursRegion } from "./modules/import/usecases/importIndicateursRegion/importIndicateursRegion.usecase";
import { importNiveauxDiplome } from "./modules/import/usecases/importNiveauxDiplome/importNiveauxDiplome.usecase";
import { importRawFile } from "./modules/import/usecases/importRawFile/importRawFile.usecase";
import { importLieuxGeographiques } from "./modules/import/usecases/importRegions/importLieuxGeographiques.usecase";

cli.command("migrateDB").action(async () => {
await migrateToLatest();
Expand Down
39 changes: 28 additions & 11 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import fastifyCors from "@fastify/cors";
import fastifySwagger from "@fastify/swagger";
import fastifySwaggerUi from "@fastify/swagger-ui";
import Boom from "@hapi/boom";
import { ZodError } from "zod";

import { config } from "../config/config";
import { logger, loggerContextPlugin } from "./logger";
import { migrateToLatest } from "./migrations/migrate";
import { extractUserInRequest, registerCoreModule } from "./modules/core";
import { registerFormationModule } from "./modules/data/index";
import { registerIntentionsModule } from "./modules/intentions/index";
import { server } from "./server";
import { registerFormationModule } from "./modules/data";
import { registerIntentionsModule } from "./modules/intentions";
import { Server, server } from "./server";
server.register(fastifyCors, {});

server.register(fastifySwagger, {
Expand All @@ -32,6 +33,15 @@ server.register(fastifySwaggerUi, {
});

server.setErrorHandler((error, _, reply) => {
if ("details" in error && error.details instanceof ZodError) {
logger.error(error.message, {
error,
details: error.details.errors,
});
reply.status(500).send({ error: "internal error", statusCode: 500 });
return;
}

if (Boom.isBoom(error)) {
reply
.status(error.output.statusCode)
Expand All @@ -44,6 +54,8 @@ server.setErrorHandler((error, _, reply) => {

if (!error.statusCode || error.statusCode >= 500) {
logger.error(error.message, { error });
reply.status(500).send({ error: "internal error", statusCode: 500 });
return;
}

if (error.statusCode && error.statusCode < 500) {
Expand Down Expand Up @@ -73,14 +85,19 @@ process.on("uncaughtExceptionMonitor", (error, origin) => {
server.addHook("onRequest", extractUserInRequest);
server.register(loggerContextPlugin);

server.register(
async (instance) => {
registerCoreModule({ server: instance });
registerFormationModule({ server: instance });
registerIntentionsModule({ server: instance });
},
{ prefix: "/api" }
);
const registerRoutes = (instance: Server) => {
return {
...registerCoreModule({ server: instance }),
...registerFormationModule({ server: instance }),
...registerIntentionsModule({ server: instance }),
};
};

export type Router = ReturnType<typeof registerRoutes>;

server.register(async (instance: Server) => registerRoutes(instance), {
prefix: "/api",
});

const cb = (error: Error | null) => {
if (error) {
Expand Down
22 changes: 18 additions & 4 deletions server/src/modules/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { Server } from "../../server";
import { authRoutes } from "./routes/auth.routes";
import { coreRoutes } from "./routes/core.routes";
import { activateUserRoute } from "./usecases/activateUser/activateUser.route";
import { checkActivationTokenRoute } from "./usecases/checkActivationToken/checkActivationToken.route";
import { homeRoute } from "./usecases/home/home.route";
import { loginRoute } from "./usecases/login/login.route";
import { logoutRoute } from "./usecases/logout/logout.route";
import { resetPasswordRoute } from "./usecases/resetPassword/resetPassword.route";
import { sendResetPasswordRoute } from "./usecases/sendResetPassword/sendResetPassword.route";
import { whoAmIRoute } from "./usecases/whoAmI/whoAmI.route";
export { extractUserInRequest } from "./utils/extractUserInRequest/extractUserInRequest";

export const registerCoreModule = ({ server }: { server: Server }) => {
authRoutes({ server });
coreRoutes({ server });
return {
...homeRoute({ server }),
...activateUserRoute(server),
...checkActivationTokenRoute(server),
...loginRoute(server),
...logoutRoute(server),
...resetPasswordRoute(server),
...sendResetPasswordRoute(server),
...whoAmIRoute(server),
};
};

export { hasPermissionHandler } from "./utils/hasPermission";
Expand Down
101 changes: 0 additions & 101 deletions server/src/modules/core/routes/auth.routes.ts

This file was deleted.

9 changes: 0 additions & 9 deletions server/src/modules/core/routes/core.routes.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createRoute } from "@http-wizard/core";

import { Server } from "../../../../server";
import { activateUserSchema } from "./activateUser.schema";
import { activateUser } from "./activateUser.usecase";

export const activateUserRoute = (server: Server) => {
return createRoute("/auth/activate", {
method: "POST",
schema: activateUserSchema,
}).handle((props) => {
server.route({
...props,
handler: async (request, response) => {
const { password, repeatPassword, activationToken } = request.body;

await activateUser({
password,
repeatPassword,
activationToken,
});
response.status(200).send();
},
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { passwordRegex } from "shared";
import { z } from "zod";
export const activateUserSchema = {
body: z.object({
password: z.string().regex(new RegExp(passwordRegex)),
repeatPassword: z.string(),
activationToken: z.string(),
}),
response: { 200: z.void() },
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createRoute } from "@http-wizard/core";

import { Server } from "../../../../server";
import { checkActivationTokenSchema } from "./checkActivationToken.schema";
import { checkActivationToken } from "./checkActivationToken.usecase";

export const checkActivationTokenRoute = (server: Server) => {
return createRoute("/auth/check-activation-token", {
method: "GET",
schema: checkActivationTokenSchema,
}).handle((props) => {
server.route({
...props,
handler: async (request, response) => {
const { activationToken } = request.query;

const valid = await checkActivationToken({
activationToken,
});
response.status(200).send({ valid });
},
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from "zod";

export const checkActivationTokenSchema = {
querystring: z.object({
activationToken: z.string(),
}),
response: {
200: z.object({
valid: z.literal(true),
}),
},
};
20 changes: 20 additions & 0 deletions server/src/modules/core/usecases/home/home.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createRoute } from "@http-wizard/core";
import { z } from "zod";

import { Server } from "../../../../server";

export const homeRoute = ({ server }: { server: Server }) => {
return createRoute("/", {
method: "GET",
schema: {
response: { 200: z.object({ hello: z.string() }) },
},
}).handle((props) => {
server.route({
...props,
handler: async (_, response) => {
response.status(200).send({ hello: "dsf" });
},
});
});
};
Loading
Loading