feat: change tariff model
This commit is contained in:
parent
6621ae1ca5
commit
c1a70eaa8d
@ -1,4 +1,4 @@
|
|||||||
FROM node:19.1-alpine as dev
|
FROM node:19.1-alpine AS dev
|
||||||
|
|
||||||
RUN apk update && rm -rf /var/cache/apk/*
|
RUN apk update && rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ RUN ls
|
|||||||
|
|
||||||
RUN yarn build
|
RUN yarn build
|
||||||
|
|
||||||
FROM node:19.1-alpine as production
|
FROM node:19.1-alpine AS production
|
||||||
|
|
||||||
RUN apk update && rm -rf /var/cache/apk/*
|
RUN apk update && rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
|
@ -10,11 +10,11 @@ services:
|
|||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
environment:
|
environment:
|
||||||
- DB_HOST=admin-mongo
|
- DB_HOST=mongo
|
||||||
- DB_PORT=27017
|
- DB_PORT=27017
|
||||||
- ENVIRONMENT=staging
|
- ENVIRONMENT=staging
|
||||||
- HTTP_HOST=0.0.0.0
|
- HTTP_HOST=0.0.0.0
|
||||||
- HTTP_PORT=8005
|
- HTTP_PORT=8001
|
||||||
- AUTH_SERVICE_HOST=http://auth
|
- AUTH_SERVICE_HOST=http://auth
|
||||||
- AUTH_SERVICE_PORT=8000
|
- AUTH_SERVICE_PORT=8000
|
||||||
- DB_USERNAME=test
|
- DB_USERNAME=test
|
||||||
@ -22,20 +22,9 @@ services:
|
|||||||
- DB_NAME=admin
|
- DB_NAME=admin
|
||||||
networks:
|
networks:
|
||||||
- dev
|
- dev
|
||||||
depends_on:
|
|
||||||
- admin-mongo
|
|
||||||
ports:
|
ports:
|
||||||
- 8005:8005
|
- 8001:8001
|
||||||
|
|
||||||
admin-mongo:
|
|
||||||
image: "mongo:6.0.3"
|
|
||||||
environment:
|
|
||||||
MONGO_INITDB_ROOT_USERNAME: test
|
|
||||||
MONGO_INITDB_ROOT_PASSWORD: test
|
|
||||||
ports:
|
|
||||||
- 27017:27017
|
|
||||||
networks:
|
|
||||||
- dev
|
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
dev:
|
dev:
|
||||||
|
@ -23,7 +23,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fastify/cookie": "^8.3.0",
|
"@fastify/cookie": "^8.3.0",
|
||||||
"@fastify/cors": "^8.2.0",
|
|
||||||
"@fastify/jwt": "^6.3.3",
|
"@fastify/jwt": "^6.3.3",
|
||||||
"@fastify/swagger": "^8.2.1",
|
"@fastify/swagger": "^8.2.1",
|
||||||
"@fastify/swagger-ui": "^1.3.0",
|
"@fastify/swagger-ui": "^1.3.0",
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import cors from "@fastify/cors";
|
|
||||||
import cookie from "@fastify/cookie";
|
import cookie from "@fastify/cookie";
|
||||||
import jwt from "@fastify/jwt";
|
import jwt from "@fastify/jwt";
|
||||||
import swagger from "@fastify/swagger";
|
import swagger from "@fastify/swagger";
|
||||||
@ -12,7 +11,6 @@ import type { FastifyInstance } from "fastify";
|
|||||||
import type { PluginsOptions } from "@/types/configuration/plugins-options";
|
import type { PluginsOptions } from "@/types/configuration/plugins-options";
|
||||||
|
|
||||||
export const registerFastifyPlugins = (fastify: FastifyInstance, options: PluginsOptions) => {
|
export const registerFastifyPlugins = (fastify: FastifyInstance, options: PluginsOptions) => {
|
||||||
fastify.register(cors, options.cors);
|
|
||||||
fastify.register(cookie, options.cookie);
|
fastify.register(cookie, options.cookie);
|
||||||
fastify.register(jwt, options.jwt);
|
fastify.register(jwt, options.jwt);
|
||||||
fastify.register(swagger, DEFAULT.swaggerOptions);
|
fastify.register(swagger, DEFAULT.swaggerOptions);
|
||||||
|
@ -9,12 +9,13 @@ import { validateTariff } from "./helpers";
|
|||||||
import type { FastifyReply } from "fastify";
|
import type { FastifyReply } from "fastify";
|
||||||
|
|
||||||
import type { Privilege } from "@/types/models/privilege.type";
|
import type { Privilege } from "@/types/models/privilege.type";
|
||||||
|
import type { Eloquent } from "@/types/models/eloquent.type";
|
||||||
import type { CreateTariffRequest, GetTariffRequest, ReplaceTariffRequest, RemoveTariffRequest } from "./types";
|
import type { CreateTariffRequest, GetTariffRequest, ReplaceTariffRequest, RemoveTariffRequest } from "./types";
|
||||||
|
|
||||||
export const getTariffs = async () => TariffModel.find({}).lean();
|
export const getTariffs = async () => TariffModel.find({}).lean();
|
||||||
|
|
||||||
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
||||||
const [requestParams, error] = validateEmptyFields(request.params || {}, ["id"]);
|
const [requestParams, error] = validateEmptyFields(request.params ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
reply.status(400);
|
reply.status(400);
|
||||||
@ -52,24 +53,22 @@ export const createTariff = async (request: CreateTariffRequest, reply: FastifyR
|
|||||||
}
|
}
|
||||||
|
|
||||||
const privilegies = await PrivilegeModel.find({ privilegeId: requestBody.privilegieIDArray }).lean();
|
const privilegies = await PrivilegeModel.find({ privilegeId: requestBody.privilegieIDArray }).lean();
|
||||||
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => ({
|
||||||
const privilegiesMap = requestBody.privilegieIDArray.reduce<Record<string, Privilege>>((accamulator, privilegeId) => {
|
name: privilege.name,
|
||||||
const findedPrivilege = privilegies.find((privilege) => privilege.privilegeId === privilegeId);
|
privilegeId: privilege.name,
|
||||||
|
serviceKey: privilege.serviceKey,
|
||||||
if (!findedPrivilege) {
|
description: privilege.description,
|
||||||
return accamulator;
|
amount: privilege.amount,
|
||||||
}
|
type: privilege.type,
|
||||||
|
value: privilege.value,
|
||||||
accamulator[privilegeId] = findedPrivilege;
|
price: privilege.price,
|
||||||
|
}));
|
||||||
return accamulator;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
const newTariff = new TariffModel({
|
const newTariff = new TariffModel({
|
||||||
name: requestBody.name,
|
name: requestBody.name,
|
||||||
price: requestBody.price,
|
price: requestBody.price,
|
||||||
isCustom: requestBody.isCustom,
|
isCustom: requestBody.isCustom,
|
||||||
privilegies: privilegiesMap,
|
privilegies: cleanPrivilegies,
|
||||||
});
|
});
|
||||||
|
|
||||||
await newTariff.save();
|
await newTariff.save();
|
||||||
@ -78,7 +77,7 @@ export const createTariff = async (request: CreateTariffRequest, reply: FastifyR
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
||||||
const [requestBody, error] = validateTariff(request.body || {});
|
const [requestBody, error] = validateTariff(request.body ?? {});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
reply.status(400);
|
reply.status(400);
|
||||||
@ -105,31 +104,29 @@ export const replaceTariff = async (request: ReplaceTariffRequest, reply: Fastif
|
|||||||
}
|
}
|
||||||
|
|
||||||
const privilegies = await PrivilegeModel.find({ privilegeId: requestBody.privilegieIDArray }).lean();
|
const privilegies = await PrivilegeModel.find({ privilegeId: requestBody.privilegieIDArray }).lean();
|
||||||
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => ({
|
||||||
const privilegiesMap = requestBody.privilegieIDArray.reduce<Record<string, Privilege>>((accamulator, privilegeId) => {
|
name: privilege.name,
|
||||||
const findedPrivilege = privilegies.find((privilege) => privilege.privilegeId === privilegeId);
|
privilegeId: privilege.name,
|
||||||
|
serviceKey: privilege.serviceKey,
|
||||||
if (!findedPrivilege) {
|
description: privilege.description,
|
||||||
return accamulator;
|
amount: privilege.amount,
|
||||||
}
|
type: privilege.type,
|
||||||
|
value: privilege.value,
|
||||||
accamulator[privilegeId] = findedPrivilege;
|
price: privilege.price,
|
||||||
|
}));
|
||||||
return accamulator;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
await tariff.replaceOne({
|
await tariff.replaceOne({
|
||||||
name: requestBody.name,
|
name: requestBody.name,
|
||||||
price: requestBody.price,
|
price: requestBody.price,
|
||||||
isCustom: requestBody.isCustom,
|
isCustom: requestBody.isCustom,
|
||||||
privilegies: privilegiesMap,
|
privilegies: cleanPrivilegies,
|
||||||
});
|
});
|
||||||
|
|
||||||
return tariff;
|
return tariff;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||||
const [{ id }, error] = validateEmptyFields(request.body || {}, ["id"]);
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
reply.status(400);
|
reply.status(400);
|
||||||
@ -154,7 +151,7 @@ export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyR
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||||
const [{ id }, error] = validateEmptyFields(request.body || {}, ["id"]);
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
reply.status(400);
|
reply.status(400);
|
||||||
@ -177,7 +174,7 @@ export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyR
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||||
const [{ id }, error] = validateEmptyFields(request.body || {}, ["id"]);
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
reply.status(400);
|
reply.status(400);
|
||||||
|
@ -11,10 +11,6 @@ const server = new Server({
|
|||||||
serverOptions: CONFIGURATION.http,
|
serverOptions: CONFIGURATION.http,
|
||||||
databaseOptions: CONFIGURATION.db,
|
databaseOptions: CONFIGURATION.db,
|
||||||
pluginsOptions: {
|
pluginsOptions: {
|
||||||
cors: {
|
|
||||||
methods: ["GET", "PUT", "POST", "PATCH", "DELETE"],
|
|
||||||
origin: "*",
|
|
||||||
},
|
|
||||||
jwt: {
|
jwt: {
|
||||||
secret: {
|
secret: {
|
||||||
public: CONFIGURATION.service.publicAccessSecretKey,
|
public: CONFIGURATION.service.publicAccessSecretKey,
|
||||||
|
@ -14,6 +14,11 @@ const schema: SchemaDefinition<Privilege> = {
|
|||||||
required: true,
|
required: true,
|
||||||
index: true,
|
index: true,
|
||||||
},
|
},
|
||||||
|
amount: {
|
||||||
|
type: Number,
|
||||||
|
require: false,
|
||||||
|
default: 1,
|
||||||
|
},
|
||||||
serviceKey: {
|
serviceKey: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -19,9 +19,8 @@ const schema: SchemaDefinition<Tariff> = {
|
|||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
privilegies: {
|
privilegies: {
|
||||||
type: Map,
|
type: [PrivilegeSchema],
|
||||||
of: PrivilegeSchema,
|
default: [],
|
||||||
default: {},
|
|
||||||
},
|
},
|
||||||
...eloquentSchema,
|
...eloquentSchema,
|
||||||
};
|
};
|
||||||
|
@ -36,8 +36,8 @@ export const tariff: SwaggerMessage = {
|
|||||||
price: { type: "number" },
|
price: { type: "number" },
|
||||||
isCustom: { type: "boolean" },
|
isCustom: { type: "boolean" },
|
||||||
privilegies: {
|
privilegies: {
|
||||||
type: "object",
|
type: "array",
|
||||||
additionalProperties: privilege,
|
items: privilege,
|
||||||
},
|
},
|
||||||
isDeleted: { type: "boolean" },
|
isDeleted: { type: "boolean" },
|
||||||
createdAt: {
|
createdAt: {
|
||||||
@ -58,8 +58,8 @@ export const tariff: SwaggerMessage = {
|
|||||||
name: "Использование сервисов",
|
name: "Использование сервисов",
|
||||||
price: 14000,
|
price: 14000,
|
||||||
isCustom: false,
|
isCustom: false,
|
||||||
privilegies: {
|
privilegies: [
|
||||||
"507f1f77bcf86cd799439011": {
|
{
|
||||||
name: "507f1f77bcf86cd799439011",
|
name: "507f1f77bcf86cd799439011",
|
||||||
privilegeId: "507f1f77bcf86cd799439011",
|
privilegeId: "507f1f77bcf86cd799439011",
|
||||||
serviceKey: "docx-templater-service",
|
serviceKey: "docx-templater-service",
|
||||||
@ -68,7 +68,7 @@ export const tariff: SwaggerMessage = {
|
|||||||
value: "200",
|
value: "200",
|
||||||
price: 12300,
|
price: 12300,
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
isDeleted: false,
|
isDeleted: false,
|
||||||
createdAt: "2017-07-21T17:32:28Z",
|
createdAt: "2017-07-21T17:32:28Z",
|
||||||
updatedAt: "2017-07-21T17:32:28Z",
|
updatedAt: "2017-07-21T17:32:28Z",
|
||||||
@ -77,8 +77,8 @@ export const tariff: SwaggerMessage = {
|
|||||||
name: "user",
|
name: "user",
|
||||||
price: 14000,
|
price: 14000,
|
||||||
isCustom: false,
|
isCustom: false,
|
||||||
privilegies: {
|
privilegies: [
|
||||||
"507f1f77bcf86cd799439011": {
|
{
|
||||||
name: "507f1f77bcf86cd799439011",
|
name: "507f1f77bcf86cd799439011",
|
||||||
privilegeId: "507f1f77bcf86cd799439011",
|
privilegeId: "507f1f77bcf86cd799439011",
|
||||||
serviceKey: "docx-templater-service",
|
serviceKey: "docx-templater-service",
|
||||||
@ -87,7 +87,7 @@ export const tariff: SwaggerMessage = {
|
|||||||
value: "200",
|
value: "200",
|
||||||
price: 12300,
|
price: 12300,
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
isDeleted: true,
|
isDeleted: true,
|
||||||
createdAt: "2017-07-21T17:32:28Z",
|
createdAt: "2017-07-21T17:32:28Z",
|
||||||
updatedAt: "2019-04-14T15:32:15Z",
|
updatedAt: "2019-04-14T15:32:15Z",
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import type { FastifyCorsOptions } from "@fastify/cors";
|
import type { FastifyCookieOptions } from "@fastify/cookie";
|
||||||
import type { FastifyCookieOptions } from "@fastify/cookie";
|
import type { FastifyJWTOptions } from "@fastify/jwt";
|
||||||
import type { FastifyJWTOptions } from "@fastify/jwt";
|
|
||||||
|
export type PluginsOptions = {
|
||||||
export type PluginsOptions = {
|
cookie?: FastifyCookieOptions;
|
||||||
cors?: FastifyCorsOptions;
|
jwt?: FastifyJWTOptions;
|
||||||
cookie?: FastifyCookieOptions;
|
};
|
||||||
jwt?: FastifyJWTOptions;
|
|
||||||
};
|
|
||||||
|
@ -5,6 +5,7 @@ export type Privilege = Eloquent & {
|
|||||||
privilegeId: string;
|
privilegeId: string;
|
||||||
serviceKey: string;
|
serviceKey: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
amount: number;
|
||||||
type: "count" | "day" | "full";
|
type: "count" | "day" | "full";
|
||||||
value: string;
|
value: string;
|
||||||
price: number;
|
price: number;
|
||||||
|
@ -5,5 +5,5 @@ export type Tariff = Eloquent & {
|
|||||||
name: string;
|
name: string;
|
||||||
price: number;
|
price: number;
|
||||||
isCustom: boolean;
|
isCustom: boolean;
|
||||||
privilegies: Record<string, Omit<Privilege, keyof Eloquent>>;
|
privilegies: Array<Omit<Privilege, keyof Eloquent>>;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user