Merge branch 'privilege-handlers' into 'dev'

feat: privilege handlers

See merge request pena-services/hub_admin_backend_service!5
This commit is contained in:
Mikhail 2022-12-24 12:18:28 +00:00
commit def2ca57d2
8 changed files with 199 additions and 6 deletions

@ -5,7 +5,7 @@ export const validatePrivilege = (privilege: RawPrivilege): Error | null => {
const typeValues: typeof privilege.type[] = ["count", "day", "full"]; const typeValues: typeof privilege.type[] = ["count", "day", "full"];
if (!typeValues.includes(privilege.type)) { if (!typeValues.includes(privilege.type)) {
return new Error("invalid 'type' value"); return new Error("invalid <type> value");
} }
if (isNaN(Number(privilege.price))) { if (isNaN(Number(privilege.price))) {

@ -11,8 +11,56 @@ import type {
GetServicePrivilegiesRequest, GetServicePrivilegiesRequest,
RegisterPrivilegeRequest, RegisterPrivilegeRequest,
GetPrivilegeRequest, GetPrivilegeRequest,
RegisterPrivilegiesRequest,
} from "./types"; } from "./types";
export const registerPrivilegies = async (request: RegisterPrivilegiesRequest, reply: FastifyReply) => {
const [requestBody, errorEmpty] = validateEmptyFields(request.body || {}, ["privilegies"]);
if (errorEmpty) {
reply.status(400);
return errorEmpty;
}
if (!requestBody.privilegies.length) {
reply.status(400);
return new Error("empty privilege array");
}
for (const rawPrivilege of requestBody.privilegies) {
const errorInvalid = validatePrivilege(rawPrivilege);
if (errorInvalid) {
reply.status(400);
return errorInvalid;
}
}
const updatePrivilegeRequests = requestBody.privilegies.map(async (privilege) => {
await PrivilegeModel.updateOne(
{ privilegeId: privilege.privilegeId },
{
$set: {
name: privilege.name,
privilegeId: privilege.privilegeId,
serviceKey: privilege.serviceKey,
description: privilege.description,
type: privilege.type,
value: privilege.value,
price: privilege.price,
isDeleted: false,
createdAt: new Date(),
},
},
{ upsert: true }
).lean();
return privilege;
});
return Promise.all(updatePrivilegeRequests);
};
export const registerPrivilege = async (request: RegisterPrivilegeRequest, reply: FastifyReply) => { export const registerPrivilege = async (request: RegisterPrivilegeRequest, reply: FastifyReply) => {
const [requestBody, errorEmpty] = validateEmptyFields( const [requestBody, errorEmpty] = validateEmptyFields(
request.body || {}, request.body || {},
@ -141,7 +189,80 @@ export const replacePrivilege = async (request: RegisterPrivilegeRequest, reply:
type: requestBody.type, type: requestBody.type,
value: requestBody.value, value: requestBody.value,
price: requestBody.price, price: requestBody.price,
updatedAt: new Date(),
}); });
return privilege; return privilege;
}; };
export const replacePrivilegies = async (request: RegisterPrivilegiesRequest, reply: FastifyReply) => {
const [requestBody, errorEmpty] = validateEmptyFields(request.body || {}, ["privilegies"]);
if (errorEmpty) {
reply.status(400);
return errorEmpty;
}
if (!requestBody.privilegies.length) {
reply.status(400);
return new Error("empty privilege array");
}
for (const rawPrivilege of requestBody.privilegies) {
const errorInvalid = validatePrivilege(rawPrivilege);
if (errorInvalid) {
reply.status(400);
return errorInvalid;
}
}
const replacePrivilegeRequests = requestBody.privilegies.map(async (privilege) => {
await PrivilegeModel.replaceOne(
{ privilegeId: privilege.privilegeId },
{
$set: {
name: privilege.name,
privilegeId: privilege.privilegeId,
serviceKey: privilege.serviceKey,
description: privilege.description,
type: privilege.type,
value: privilege.value,
price: privilege.price,
updatedAt: new Date(),
isDeleted: false,
},
}
).lean();
return privilege;
});
return Promise.all(replacePrivilegeRequests);
};
export const removePrivilege = async (request: GetPrivilegeRequest, reply: FastifyReply) => {
const [requestParams, error] = validateEmptyFields(request.params || {}, ["id"]);
if (error) {
reply.status(400);
return error;
}
if (!Types.ObjectId.isValid(requestParams.id)) {
reply.status(400);
return new Error("invalid id");
}
const privilege = await PrivilegeModel.findOneAndUpdate(
{ privilegeId: requestParams.id },
{ $set: { isDeleted: true, deletedAt: new Date() } }
);
if (!privilege) {
reply.status(404);
return new Error("privilege not found");
}
return privilege;
};

@ -22,6 +22,12 @@ export type RegisterPrivilegeRequest = FastifyRequest<{
Body?: RawPrivilege; Body?: RawPrivilege;
}>; }>;
export type RegisterPrivilegiesRequest = FastifyRequest<{
Body?: {
privilegies?: RawPrivilege[];
};
}>;
export type GetServicePrivilegiesRequest = FastifyRequest<{ export type GetServicePrivilegiesRequest = FastifyRequest<{
Params?: { Params?: {
serviceKey?: string; serviceKey?: string;

@ -6,6 +6,9 @@ import {
getPrivilege, getPrivilege,
getServicePrivilegies, getServicePrivilegies,
replacePrivilege, replacePrivilege,
registerPrivilegies,
removePrivilege,
replacePrivilegies,
} from "@/handlers/privilege"; } from "@/handlers/privilege";
import { import {
@ -13,7 +16,10 @@ import {
getPrivilegeSchema, getPrivilegeSchema,
getServicePrivilegiesSchema, getServicePrivilegiesSchema,
registerPrivilegeSchema, registerPrivilegeSchema,
registerPrivilegiesSchema,
replacePrivilegeSchema, replacePrivilegeSchema,
replacePrivilegiesSchema,
removePrivilegeSchema,
} from "@/swagger/privilege"; } from "@/swagger/privilege";
export const setPrivilegeRoutes = (router: Router): void => { export const setPrivilegeRoutes = (router: Router): void => {
@ -21,5 +27,8 @@ export const setPrivilegeRoutes = (router: Router): void => {
router.get("/:id", getPrivilege, { schema: getPrivilegeSchema }); router.get("/:id", getPrivilege, { schema: getPrivilegeSchema });
router.get("/service/:serviceKey", getServicePrivilegies, { schema: getServicePrivilegiesSchema }); router.get("/service/:serviceKey", getServicePrivilegies, { schema: getServicePrivilegiesSchema });
router.post("/", registerPrivilege, { schema: registerPrivilegeSchema }); router.post("/", registerPrivilege, { schema: registerPrivilegeSchema });
router.post("/many", registerPrivilegies, { schema: registerPrivilegiesSchema });
router.put("/", replacePrivilege, { schema: replacePrivilegeSchema }); router.put("/", replacePrivilege, { schema: replacePrivilegeSchema });
router.put("/many", replacePrivilegies, { schema: replacePrivilegiesSchema });
router.delete("/", removePrivilege, { schema: removePrivilegeSchema });
}; };

@ -46,7 +46,7 @@ export class Router {
public post = <T extends RouteGenericInterface>( public post = <T extends RouteGenericInterface>(
path: string, path: string,
handler: HandlerMethod<T>, handler: HandlerMethod<T>,
options: RouteShorthandOptions options: RouteShorthandOptions = {}
) => { ) => {
this.fastifyInstance.post(path, options, handler); this.fastifyInstance.post(path, options, handler);
}; };
@ -54,7 +54,7 @@ export class Router {
public put = <T extends RouteGenericInterface>( public put = <T extends RouteGenericInterface>(
path: string, path: string,
handler: HandlerMethod<T>, handler: HandlerMethod<T>,
options: RouteShorthandOptions options: RouteShorthandOptions = {}
) => { ) => {
this.fastifyInstance.put(path, options, handler); this.fastifyInstance.put(path, options, handler);
}; };
@ -62,7 +62,7 @@ export class Router {
public patch = <T extends RouteGenericInterface>( public patch = <T extends RouteGenericInterface>(
path: string, path: string,
handler: HandlerMethod<T>, handler: HandlerMethod<T>,
options: RouteShorthandOptions options: RouteShorthandOptions = {}
) => { ) => {
this.fastifyInstance.patch(path, options, handler); this.fastifyInstance.patch(path, options, handler);
}; };
@ -70,7 +70,7 @@ export class Router {
public delete = <T extends RouteGenericInterface>( public delete = <T extends RouteGenericInterface>(
path: string, path: string,
handler: HandlerMethod<T>, handler: HandlerMethod<T>,
options: RouteShorthandOptions options: RouteShorthandOptions = {}
) => { ) => {
this.fastifyInstance.delete(path, options, handler); this.fastifyInstance.delete(path, options, handler);
}; };

@ -1,10 +1,19 @@
import { privilegeBody, getPrivilegeParams, getServicePrivilegiesParams, getPrivilegiesQuery } from "./inputs"; import {
privilegeBody,
privilegiesBody,
getPrivilegeParams,
getServicePrivilegiesParams,
getPrivilegiesQuery,
} from "./inputs";
import { import {
getPrivilegeReponse, getPrivilegeReponse,
getPrivilegiesReponse, getPrivilegiesReponse,
getAllPrivilegiesReponse, getAllPrivilegiesReponse,
registerPrivilegeResponse, registerPrivilegeResponse,
replacePrivilegeResponse, replacePrivilegeResponse,
registerPrivilegiesResponse,
replacePrivilegiesResponse,
removePrivilegeResponse,
} from "./responses"; } from "./responses";
import type { SwaggerSchema } from "@/types/swagger.type"; import type { SwaggerSchema } from "@/types/swagger.type";
@ -38,9 +47,30 @@ export const registerPrivilegeSchema: SwaggerSchema = {
response: registerPrivilegeResponse, response: registerPrivilegeResponse,
}; };
export const registerPrivilegiesSchema: SwaggerSchema = {
summary: "Регистрация привелегий сервиса",
tags: ["privilege"],
body: privilegiesBody,
response: registerPrivilegiesResponse,
};
export const replacePrivilegeSchema: SwaggerSchema = { export const replacePrivilegeSchema: SwaggerSchema = {
summary: "Замена привилегии сервиса", summary: "Замена привилегии сервиса",
tags: ["privilege"], tags: ["privilege"],
body: privilegeBody, body: privilegeBody,
response: replacePrivilegeResponse, response: replacePrivilegeResponse,
}; };
export const replacePrivilegiesSchema: SwaggerSchema = {
summary: "Замена привилегий сервиса",
tags: ["privilege"],
body: privilegiesBody,
response: replacePrivilegiesResponse,
};
export const removePrivilegeSchema: SwaggerSchema = {
summary: "Удаление привелегии",
tags: ["privilege"],
body: getPrivilegeParams,
response: removePrivilegeResponse,
};

@ -25,6 +25,11 @@ export const privilegeBody: SwaggerMessage = {
], ],
}; };
export const privilegiesBody: SwaggerMessage = {
type: "array",
items: privilegeBody,
};
export const getPrivilegeParams: SwaggerMessage = { export const getPrivilegeParams: SwaggerMessage = {
type: "object", type: "object",
required: ["id"], required: ["id"],

@ -28,8 +28,30 @@ export const registerPrivilegeResponse: Record<string, SwaggerMessage> = {
409: swaggerError(409, "privilege already exist"), 409: swaggerError(409, "privilege already exist"),
}; };
export const registerPrivilegiesResponse: Record<string, SwaggerMessage> = {
200: {
type: "array",
items: privilege,
},
400: swaggerError(400, "price must be a number"),
};
export const replacePrivilegeResponse: Record<string, SwaggerMessage> = { export const replacePrivilegeResponse: Record<string, SwaggerMessage> = {
200: privilege, 200: privilege,
400: swaggerError(400, "invalid 'type' value"), 400: swaggerError(400, "invalid 'type' value"),
404: swaggerError(404, "privilege not found"), 404: swaggerError(404, "privilege not found"),
}; };
export const replacePrivilegiesResponse: Record<string, SwaggerMessage> = {
200: {
type: "array",
items: privilege,
},
400: swaggerError(400, "invalid 'type' value"),
};
export const removePrivilegeResponse: Record<string, SwaggerMessage> = {
200: privilege,
400: swaggerError(400, "invalid id"),
404: swaggerError(404, "privilege not found"),
};