|
@@ -0,0 +1,188 @@
|
|
|
+// router
|
|
|
+import express from "express";
|
|
|
+const router = express.Router();
|
|
|
+export default router;
|
|
|
+
|
|
|
+// db
|
|
|
+import { db } from "#db";
|
|
|
+import { ZDbShema } from "#db-shema";
|
|
|
+import { sql } from "slonik";
|
|
|
+
|
|
|
+// api
|
|
|
+import { CompanyManagementApi } from "#api/companies-management-api.js";
|
|
|
+
|
|
|
+// error
|
|
|
+// import { ApiError } from "#exceptions/api-error.ts";
|
|
|
+
|
|
|
+// dayjs
|
|
|
+import dayjs from "dayjs";
|
|
|
+import utc from "dayjs/plugin/utc.js";
|
|
|
+dayjs.extend(utc);
|
|
|
+import timezone from "dayjs/plugin/timezone.js";
|
|
|
+dayjs.extend(timezone);
|
|
|
+
|
|
|
+// other
|
|
|
+import { z } from "zod";
|
|
|
+
|
|
|
+import { v7 as uuidv7 } from "uuid";
|
|
|
+// import { logger } from "#logger";
|
|
|
+import { UserUtils } from "#utils/user-utils.js";
|
|
|
+import { EntityesService } from "#modules/entities-management/entityes-service.js";
|
|
|
+import { RouterUtils } from "#utils/router-utils.js";
|
|
|
+import { ApiError } from "#exceptions/api-error.js";
|
|
|
+import { CompaniesService } from "./companies-service.js";
|
|
|
+import { CheckPermissionsService } from "#modules/permissions-management/check-permissions-service.js";
|
|
|
+
|
|
|
+dayjs.extend(utc);
|
|
|
+
|
|
|
+// TODO транзакции
|
|
|
+router.post("/create-company", async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ // валидация запроса
|
|
|
+ const { name, timezone } = CompanyManagementApi.ZCreateCompany.req.parse(
|
|
|
+ req.body,
|
|
|
+ );
|
|
|
+
|
|
|
+ const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
+
|
|
|
+ const companyId = uuidv7();
|
|
|
+
|
|
|
+ // entity
|
|
|
+ await EntityesService.createEntity(companyId, "company");
|
|
|
+
|
|
|
+ // company
|
|
|
+ await db.query(
|
|
|
+ sql.unsafe`
|
|
|
+ insert into companies_management.companies
|
|
|
+ (company_id, name, owner_id, timezone)
|
|
|
+ values
|
|
|
+ (${companyId}, ${name}, ${userId}, ${timezone})`,
|
|
|
+ );
|
|
|
+
|
|
|
+ // add user to company
|
|
|
+ await db.query(
|
|
|
+ sql.unsafe`
|
|
|
+ insert into companies_management.company_users
|
|
|
+ (company_id, user_id)
|
|
|
+ values
|
|
|
+ (${companyId}, ${userId})`,
|
|
|
+ );
|
|
|
+
|
|
|
+ // permissions
|
|
|
+ const MANAGER_ROLE_ID = process.env.COMPANY_DEFAULT_ROLE_MANAGER_ID;
|
|
|
+ if (!MANAGER_ROLE_ID) {
|
|
|
+ throw Error("COMPANY_DEFAULT_ROLE_MANAGER_ID is not defined");
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO сделать триггер для проверка entity_id и is_defaul в БД при создании роли.
|
|
|
+ await db.query(
|
|
|
+ sql.unsafe`
|
|
|
+ insert into permissions_management.user_roles
|
|
|
+ (user_id, role_id, entity_id)
|
|
|
+ values
|
|
|
+ (${userId}, ${MANAGER_ROLE_ID}, ${companyId})`,
|
|
|
+ );
|
|
|
+
|
|
|
+ RouterUtils.validAndSendResponse(
|
|
|
+ CompanyManagementApi.ZCreateCompany.res,
|
|
|
+ res,
|
|
|
+ { code: "success" },
|
|
|
+ );
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+router.post("/get-user-companies", async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
+
|
|
|
+ const companies = await db.any(
|
|
|
+ sql.type(
|
|
|
+ z.object({
|
|
|
+ company_id: ZDbShema.companies_management.companies.company_id,
|
|
|
+ name: ZDbShema.companies_management.companies.name,
|
|
|
+ owner_id: ZDbShema.companies_management.companies.owner_id,
|
|
|
+ timezone: ZDbShema.companies_management.companies.timezone,
|
|
|
+ }),
|
|
|
+ )`
|
|
|
+ select
|
|
|
+ c.company_id,
|
|
|
+ c."name",
|
|
|
+ c.owner_id,
|
|
|
+ c.timezone
|
|
|
+ from
|
|
|
+ companies_management.companies c
|
|
|
+ join companies_management.company_users cu on
|
|
|
+ c.company_id = cu.company_id
|
|
|
+ where
|
|
|
+ cu.user_id = ${userId}
|
|
|
+ `,
|
|
|
+ );
|
|
|
+
|
|
|
+ RouterUtils.validAndSendResponse(
|
|
|
+ CompanyManagementApi.ZGetUserCompanies.res,
|
|
|
+ res,
|
|
|
+ { code: "success", companies: [...companies] },
|
|
|
+ );
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+router.post("/get-company", async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ // валидация запроса
|
|
|
+ const { companyId } = CompanyManagementApi.ZGetCompany.req.parse(req.body);
|
|
|
+
|
|
|
+ const userId = UserUtils.getUserFromReq(req).userId;
|
|
|
+
|
|
|
+ await CheckPermissionsService.checkEntityPermission(
|
|
|
+ companyId,
|
|
|
+ userId,
|
|
|
+ "view_company",
|
|
|
+ "view_company_true",
|
|
|
+ );
|
|
|
+
|
|
|
+ const company = await db.maybeOne(
|
|
|
+ sql.type(
|
|
|
+ z.object({
|
|
|
+ company_id: ZDbShema.companies_management.companies.company_id,
|
|
|
+ name: ZDbShema.companies_management.companies.name,
|
|
|
+ owner_id: ZDbShema.companies_management.companies.owner_id,
|
|
|
+ timezone: ZDbShema.companies_management.companies.timezone,
|
|
|
+ }),
|
|
|
+ )`
|
|
|
+ select
|
|
|
+ c.company_id,
|
|
|
+ c."name",
|
|
|
+ c.owner_id,
|
|
|
+ c.timezone
|
|
|
+ from
|
|
|
+ companies_management.companies c
|
|
|
+ where
|
|
|
+ c.company_id = ${companyId}
|
|
|
+ `,
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!company) {
|
|
|
+ throw ApiError.BadRequest("Company not found", "Компания не найдена");
|
|
|
+ }
|
|
|
+
|
|
|
+ const REQUIRED_PERMISSION = "view_event_true";
|
|
|
+
|
|
|
+ const events = await CompaniesService.getUserCompanyEvents(
|
|
|
+ userId,
|
|
|
+ company.company_id,
|
|
|
+ REQUIRED_PERMISSION,
|
|
|
+ );
|
|
|
+
|
|
|
+ RouterUtils.validAndSendResponse(
|
|
|
+ CompanyManagementApi.ZGetCompany.res,
|
|
|
+ res,
|
|
|
+ { code: "success", company: { ...company, events: [...events] } },
|
|
|
+ );
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+});
|