fosrl.pangolin/server/routers/resource/setResourcePincode.ts

107 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-11-23 20:08:56 -05:00
import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
2025-06-04 12:02:07 -04:00
import { resourcePincode } from "@server/db";
2024-11-23 20:08:56 -05:00
import { eq } from "drizzle-orm";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import { fromError } from "zod-validation-error";
import { hash } from "@node-rs/argon2";
2025-01-01 21:41:31 -05:00
import { response } from "@server/lib";
import stoi from "@server/lib/stoi";
2024-12-21 21:01:12 -05:00
import logger from "@server/logger";
2024-12-22 16:59:30 -05:00
import { hashPassword } from "@server/auth/password";
2025-04-06 22:44:14 -04:00
import { OpenAPITags, registry } from "@server/openApi";
2024-11-23 20:08:56 -05:00
const setResourceAuthMethodsParamsSchema = z.object({
2025-04-06 22:44:14 -04:00
resourceId: z.string().transform(Number).pipe(z.number().int().positive())
2024-11-23 20:08:56 -05:00
});
const setResourceAuthMethodsBodySchema = z
.object({
pincode: z
.string()
.regex(/^\d{6}$/)
2025-04-06 22:44:14 -04:00
.or(z.null())
2024-11-23 20:08:56 -05:00
})
.strict();
2025-04-06 22:44:14 -04:00
registry.registerPath({
method: "post",
path: "/resource/{resourceId}/pincode",
description:
"Set the PIN code for a resource. Setting the PIN code to null will remove it.",
tags: [OpenAPITags.Resource],
request: {
params: setResourceAuthMethodsParamsSchema,
body: {
content: {
"application/json": {
schema: setResourceAuthMethodsBodySchema
}
}
}
},
responses: {}
});
2024-11-23 20:08:56 -05:00
export async function setResourcePincode(
req: Request,
res: Response,
2025-04-06 22:44:14 -04:00
next: NextFunction
2024-11-23 20:08:56 -05:00
): Promise<any> {
try {
const parsedParams = setResourceAuthMethodsParamsSchema.safeParse(
2025-04-06 22:44:14 -04:00
req.params
2024-11-23 20:08:56 -05:00
);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2025-04-06 22:44:14 -04:00
fromError(parsedParams.error).toString()
)
2024-11-23 20:08:56 -05:00
);
}
const parsedBody = setResourceAuthMethodsBodySchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2025-04-06 22:44:14 -04:00
fromError(parsedBody.error).toString()
)
2024-11-23 20:08:56 -05:00
);
}
const { resourceId } = parsedParams.data;
const { pincode } = parsedBody.data;
await db.transaction(async (trx) => {
await trx
.delete(resourcePincode)
.where(eq(resourcePincode.resourceId, resourceId));
if (pincode) {
2024-12-22 16:59:30 -05:00
const pincodeHash = await hashPassword(pincode);
2024-11-23 20:08:56 -05:00
await trx
.insert(resourcePincode)
.values({ resourceId, pincodeHash, digitLength: 6 });
}
});
return response(res, {
data: {},
success: true,
error: false,
message: "Resource PIN code set successfully",
2025-04-06 22:44:14 -04:00
status: HttpCode.CREATED
2024-11-23 20:08:56 -05:00
});
} catch (error) {
2024-12-21 21:01:12 -05:00
logger.error(error);
2024-11-23 20:08:56 -05:00
return next(
2025-04-06 22:44:14 -04:00
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
2024-11-23 20:08:56 -05:00
);
}
}