123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- import {
- CustomFieldWithUserCopyValue,
- CustomFieldWithValidators,
- CustomFieldWithValue,
- } from "#api/v_0.1.0/types/custom-fields-types.js";
- import { DbSchema } from "#db/db-schema.js";
- import { selPool } from "#db/db.js";
- import { sql } from "slonik";
- import { z } from "zod";
- class CPeService {
- async checkPeOwner(userId: string, peId: string) {
- return await selPool.exists(sql.unsafe`
- select
- pe_id
- from
- act.part_entities pe
- where
- pe.owner_id = ${userId}
- and pe.pe_id = ${peId}
- `);
- }
- async getPeWithValues(peId: string) {
- return await selPool.maybeOne(sql.type(
- z.object({
- peId: z.string().uuid(),
- peTypeId: z.string().uuid(),
- peTypeCode: z.string(),
- peTypeName: z.string(),
- eventInstId: z.string().uuid(),
- ownerId: z.string().uuid(),
- name: z.string(),
- fields: z.array(
- CustomFieldWithValue.extend({
- peFfId: z.string().uuid(),
- }),
- ),
- }),
- )`
- select
- pe_id "peId",
- pe_type_id "peTypeId",
- pe_type_code "peTypeCode",
- pe_type_name "peTypeName",
- event_inst_id "eventInstId",
- name,
- owner_id "ownerId",
- fields
- from
- act.pe_with_fields_and_values
- where
- pe_id = ${peId}
- `);
- }
- async getSimplePe(peId: string) {
- return await selPool.maybeOne(sql.type(
- z.object({
- peMemberId: DbSchema.act.peMembers.peMemberId,
- peId: DbSchema.act.partEntities.peId,
- peTypeId: DbSchema.act.partEntities.peTypeId,
- peTypeCode: DbSchema.act.peTypes.code,
- peTypeName: DbSchema.act.peTypes.name,
- name: DbSchema.act.partEntities.name,
- eventInstId: DbSchema.act.partEntities.eventInstId,
- ownerId: DbSchema.act.partEntities.ownerId,
- }),
- )`
- select
- pm.pe_member_id "peMemberId",
- pe.pe_id "peId",
- pt.pe_type_id "peTypeId",
- pt.code "peTypeCode",
- pt.name "peTypeName",
- pe.event_inst_id "eventInstId",
- pe.owner_id "ownerId",
- pe.name
- from
- act.pe_members pm
- left join act.part_entities pe on
- pe.pe_id = pm.pe_member_id
- left join act.pe_types pt on
- pt.pe_type_id = pe.pe_type_id
- where
- pe.pe_id = ${peId}
- `);
- }
- async getInvites(peId: string) {
- return await selPool.any(sql.type(
- z.object({
- peInviteId: DbSchema.act.peInvites.peInviteId,
- peInviteUuid: DbSchema.act.peInvites.peInviteUuid,
- name: DbSchema.act.peInvites.name,
- limitVal: DbSchema.act.peInvites.limitVal,
- countVal: DbSchema.act.peInvites.countVal,
- expirationDate: DbSchema.act.peInvites.expirationDate,
- }),
- )`
- select
- pe_invite_id "peInviteId",
- pe_invite_uuid "peInviteUuid",
- name,
- limit_val "limitVal",
- count_val "countVal",
- expiration_date "expirationDate"
- from
- act.pe_invites
- where
- pe_id = ${peId}
- `);
- }
- async getMembers(peId: string) {
- return await selPool.any(sql.type(
- z.object({
- peMemberId: z.string().uuid(),
- userId: z.string().uuid(),
- email: z.string().email(),
- fields: z.array(
- CustomFieldWithValue.extend({
- userEfId: z.string().uuid(),
- }),
- ),
- }),
- )`
- select
- pe_member_id "peMemberId",
- user_id "userId",
- email,
- fields
- from
- act.pe_members_with_fields_and_values
- where
- pe_id = ${peId}
- `);
- }
- async getInviteInfo(peInviteUuid: string) {
- return await selPool.maybeOne(sql.type(
- z.object({
- peInviteId: DbSchema.act.peInvites.peInviteId,
- peInviteUuid: DbSchema.act.peInvites.peInviteUuid,
- peId: DbSchema.act.peInvites.peId,
- peName: DbSchema.act.partEntities.name,
- peOwnerId: DbSchema.act.partEntities.ownerId,
- limitVal: DbSchema.act.peInvites.limitVal,
- countVal: DbSchema.act.peInvites.countVal,
- }),
- )`
- select
- i.pe_invite_id "peInviteId",
- i.pe_invite_uuid "peInviteUuid",
- i.pe_id "peId",
- pe."name" "peName",
- pe.owner_id "peOwnerId",
- i.limit_val "limitVal",
- i.count_val "countVal"
- from
- act.pe_invites i
- join act.part_entities pe on
- pe.pe_id = i.pe_id
- where
- pe_invite_uuid = ${peInviteUuid}
- `);
- }
- async getPeTypeWithFieldsAndUserCopyValues(
- userId: string,
- peTypeCode: string,
- eventId: string,
- ) {
- return await selPool.maybeOne(sql.type(
- z.object({
- peTypeId: DbSchema.act.peTypes.peTypeId,
- code: DbSchema.act.peTypes.code,
- name: DbSchema.act.peTypes.name,
- eventInstId: DbSchema.act.peTypes.eventInstId,
- fields: z.array(
- CustomFieldWithUserCopyValue.extend({ peFfId: z.string() }),
- ),
- }),
- )`
- select
- pt.pe_type_id as "peTypeId",
- pt.code,
- pt.name,
- pt.event_inst_id as "eventInstId",
- coalesce(jsonb_agg(jsonb_build_object(
- 'fieldDefinitionId',
- cfwv.field_definition_id,
- 'peFfId',
- pff.pe_ff_id,
- 'isCopyUserValue',
- pff.is_copy_user_value,
- 'code',
- cfwv.code,
- 'userCopyValue',
- ufwv.value,
- 'fieldTypeCode',
- cfwv.field_type_code,
- 'title',
- COALESCE(pff.field_title_override, cfwv.title),
- 'mask',
- cfwv.mask,
- 'options',
- cfwv.options,
- 'validators',
- cfwv.validators
- )) filter (
- where
- cfwv.field_definition_id is not null),
- '[]'::jsonb) as fields
- from
- act.pe_types pt
- -- необходимые поля
- left join act.pe_form_fields pff on
- pff.pe_type_id = pt.pe_type_id
- -- значение из профиля юзера
- left join ev.user_fields_with_values ufwv on
- pff.field_definition_id = ufwv.field_definition_id
- and ufwv.user_id = ${userId}
- and ufwv.event_id = ${eventId}
- and ufwv.value is not null
- -- только если нужно копировать
- and pff.is_copy_user_value = true
- left join cf.custom_fields_with_validators cfwv on
- pff.field_definition_id = cfwv.field_definition_id
- where
- pt.code = ${peTypeCode}
- group by
- pt.pe_type_id,
- pt.code,
- pt.name,
- pt.event_inst_id
- `);
- }
- async getPeTypeWithFields(peTypeCode: string) {
- return await selPool.maybeOne(sql.type(
- z.object({
- peTypeId: DbSchema.act.peTypes.peTypeId,
- code: DbSchema.act.peTypes.code,
- name: DbSchema.act.peTypes.name,
- eventInstId: DbSchema.act.peTypes.eventInstId,
- fields: z.array(
- CustomFieldWithValidators.extend({ peFfId: z.string() }),
- ),
- }),
- )`
- select
- pt.pe_type_id as "peTypeId",
- pt.code,
- pt.name,
- pt.event_inst_id as "eventInstId",
- coalesce(jsonb_agg(jsonb_build_object(
- 'fieldDefinitionId',
- cfwv.field_definition_id,
- 'peFfId',
- pff.pe_ff_id,
- 'code',
- cfwv.code,
- 'fieldTypeCode',
- cfwv.field_type_code,
- 'title',
- COALESCE(pff.field_title_override, cfwv.title),
- 'mask',
- cfwv.mask,
- 'options',
- cfwv.options,
- 'validators',
- cfwv.validators
- )) filter (
- where
- cfwv.field_definition_id is not null),
- '[]'::jsonb) as fields
- from
- act.pe_types pt
- -- необходимые поля
- left join act.pe_form_fields pff on
- pff.pe_type_id = pt.pe_type_id
- left join cf.custom_fields_with_validators cfwv on
- pff.field_definition_id = cfwv.field_definition_id
- where
- pt.code = ${peTypeCode}
- group by
- pt.pe_type_id,
- pt.code,
- pt.name,
- pt.event_inst_id
- `);
- }
- }
- export const cPeService = new CPeService();
|