|
@@ -4,7 +4,7 @@ import { DbSchema } from "#db-schema";
|
|
|
import { sql } from "slonik";
|
|
|
|
|
|
// api
|
|
|
-import { api } from "#api";
|
|
|
+import { api, apiTypes } from "#api";
|
|
|
|
|
|
// other
|
|
|
import { z } from "zod";
|
|
@@ -13,7 +13,6 @@ import { RouterUtils } from "#utils/router-utils.js";
|
|
|
|
|
|
import { Request, Response } from "express";
|
|
|
import sessionService from "#modules/users/auth/services/session-service.js";
|
|
|
-import { CustomFieldWithValue } from "#api/v_0.1.0/types/custom-fields-types.js";
|
|
|
import { ApiError } from "#exceptions/api-error.js";
|
|
|
import { v4, v7 } from "uuid";
|
|
|
import { cPeService } from "./c-pe-service.js";
|
|
@@ -149,6 +148,100 @@ class ClientPeController {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ async getPeForPatch(req: Request, res: Response) {
|
|
|
+ const { peId } = api.client.pe.GET_PeForPatch.req.parse(req.params);
|
|
|
+
|
|
|
+ const user = sessionService.getUserFromReq(req);
|
|
|
+
|
|
|
+ const pe = await cPeService.getPeWithValidatorsAndValues(peId);
|
|
|
+ if (!pe)
|
|
|
+ throw ApiError.BadRequest("peNotFound", "Сущность участия не найдена");
|
|
|
+
|
|
|
+ if (pe.ownerId !== user.userId) throw ApiError.ForbiddenError();
|
|
|
+
|
|
|
+ RouterUtils.validAndSendResponse(api.client.pe.GET_PeForPatch.res, res, {
|
|
|
+ code: "success",
|
|
|
+ pe: pe,
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ async patchPe(req: Request, res: Response) {
|
|
|
+ const { peId } = api.client.pe.PATCH_PartEntity.req.params.parse(
|
|
|
+ req.params,
|
|
|
+ );
|
|
|
+ const { name, fields } =
|
|
|
+ api.client.pe.PATCH_PartEntity.req.formData.body.parse(
|
|
|
+ JSON.parse(req.body.body),
|
|
|
+ );
|
|
|
+
|
|
|
+ const files = req.files;
|
|
|
+ const user = sessionService.getUserFromReq(req);
|
|
|
+
|
|
|
+ const peType = await cPeService.getPeWithValidatorsAndValues(peId);
|
|
|
+ if (!peType)
|
|
|
+ throw ApiError.BadRequest(
|
|
|
+ "peTypeNotFound",
|
|
|
+ "Тип сущности участия не найден",
|
|
|
+ );
|
|
|
+
|
|
|
+ // проверка доступа
|
|
|
+ if (peType.ownerId !== user.userId) throw ApiError.ForbiddenError();
|
|
|
+
|
|
|
+ const refFields = peType.fields
|
|
|
+ .map((f) => ({
|
|
|
+ ...f,
|
|
|
+ idKey: "peFfId",
|
|
|
+ }))
|
|
|
+ // только изменяемые
|
|
|
+ .filter((f) => fields.some((ff) => ff.peFfId === f.peFfId));
|
|
|
+
|
|
|
+ // валидация
|
|
|
+ const validationResult =
|
|
|
+ await cCustomFieldsValidateService.processAndValidateFields({
|
|
|
+ inputFields: fields,
|
|
|
+ referenceFields: refFields,
|
|
|
+ files,
|
|
|
+ idKey: "peFfId",
|
|
|
+ addOldValue: true,
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!validationResult.isValid)
|
|
|
+ throw ApiError.BadRequest(
|
|
|
+ "fieldsValidationFailed",
|
|
|
+ JSON.stringify(validationResult.messages),
|
|
|
+ );
|
|
|
+
|
|
|
+ const validatedFields = validationResult.checkedfields;
|
|
|
+
|
|
|
+ //
|
|
|
+ //
|
|
|
+ // вставляем в базу и сохраняем файлы
|
|
|
+ await updPool.transaction(async (tr) => {
|
|
|
+ if (name) {
|
|
|
+ await tr.query(sql.unsafe`
|
|
|
+ update act.part_entities
|
|
|
+ set
|
|
|
+ name = ${name}
|
|
|
+ where
|
|
|
+ pe_id = ${peId}
|
|
|
+ `);
|
|
|
+ }
|
|
|
+
|
|
|
+ await cCustomFieldsValidateService.saveCustomFieldValuesInTransaction({
|
|
|
+ tr,
|
|
|
+ parentId: peId,
|
|
|
+ action: "peCreate",
|
|
|
+ inputFields: validatedFields,
|
|
|
+ files,
|
|
|
+ isDeleteBefore: true,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ RouterUtils.validAndSendResponse(api.client.pe.PATCH_PartEntity.res, res, {
|
|
|
+ code: "success",
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
async getMyPes(req: Request, res: Response) {
|
|
|
const user = sessionService.getUserFromReq(req);
|
|
|
const event = await sessionService.getCurrentEventFromReq(req);
|
|
@@ -272,7 +365,7 @@ class ClientPeController {
|
|
|
|
|
|
if (!isMember) throw ApiError.ForbiddenError();
|
|
|
|
|
|
- const pe = await cPeService.getSimplePe(peId);
|
|
|
+ const pe = await cPeService.getPeForMember(peId);
|
|
|
if (!pe)
|
|
|
throw ApiError.BadRequest("peNotFound", "Сущность участия не найдена");
|
|
|
|
|
@@ -417,34 +510,7 @@ class ClientPeController {
|
|
|
|
|
|
const event = await sessionService.getCurrentEventFromReq(req);
|
|
|
|
|
|
- const pes = await selPool.any(sql.type(
|
|
|
- z.object({
|
|
|
- peId: DbSchema.act.partEntities.peId,
|
|
|
- peTypeId: DbSchema.act.partEntities.peTypeId,
|
|
|
- peTypeCode: DbSchema.act.peTypes.code,
|
|
|
- peTypeName: DbSchema.act.peTypes.name,
|
|
|
- eventInstId: DbSchema.act.partEntities.eventInstId,
|
|
|
- ownerId: DbSchema.act.partEntities.ownerId,
|
|
|
- name: DbSchema.act.partEntities.name,
|
|
|
- members: z.array(
|
|
|
- z.object({
|
|
|
- peMemberId: DbSchema.act.peMembers.peMemberId,
|
|
|
- userId: DbSchema.usr.users.userId,
|
|
|
- email: DbSchema.usr.users.email,
|
|
|
- fields: z.array(
|
|
|
- CustomFieldWithValue.extend({
|
|
|
- userEfId: z.string().uuid(),
|
|
|
- }),
|
|
|
- ),
|
|
|
- }),
|
|
|
- ),
|
|
|
- fields: z.array(
|
|
|
- CustomFieldWithValue.extend({
|
|
|
- peFfId: z.string().uuid(),
|
|
|
- }),
|
|
|
- ),
|
|
|
- }),
|
|
|
- )`
|
|
|
+ const pes = await selPool.any(sql.type(apiTypes.activities.PeForActivity)`
|
|
|
select
|
|
|
pe.pe_id "peId",
|
|
|
pe.event_inst_id "eventInstId",
|