c-pe-service.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import {
  2. CustomFieldWithUserCopyValue,
  3. CustomFieldWithValidators,
  4. CustomFieldWithValue,
  5. } from "#api/v_0.1.0/types/custom-fields-types.js";
  6. import { DbSchema } from "#db/db-schema.js";
  7. import { selPool } from "#db/db.js";
  8. import { sql } from "slonik";
  9. import { z } from "zod";
  10. class CPeService {
  11. async checkPeOwner(userId: string, peId: string) {
  12. return await selPool.exists(sql.unsafe`
  13. select
  14. pe_id
  15. from
  16. act.part_entities pe
  17. where
  18. pe.owner_id = ${userId}
  19. and pe.pe_id = ${peId}
  20. `);
  21. }
  22. async getPeWithValues(peId: string) {
  23. return await selPool.maybeOne(sql.type(
  24. z.object({
  25. peId: z.string().uuid(),
  26. peTypeId: z.string().uuid(),
  27. peTypeCode: z.string(),
  28. peTypeName: z.string(),
  29. eventInstId: z.string().uuid(),
  30. ownerId: z.string().uuid(),
  31. name: z.string(),
  32. fields: z.array(
  33. CustomFieldWithValue.extend({
  34. peFfId: z.string().uuid(),
  35. }),
  36. ),
  37. }),
  38. )`
  39. select
  40. pe_id "peId",
  41. pe_type_id "peTypeId",
  42. pe_type_code "peTypeCode",
  43. pe_type_name "peTypeName",
  44. event_inst_id "eventInstId",
  45. name,
  46. owner_id "ownerId",
  47. fields
  48. from
  49. act.pe_with_fields_and_values
  50. where
  51. pe_id = ${peId}
  52. `);
  53. }
  54. async getSimplePe(peId: string) {
  55. return await selPool.maybeOne(sql.type(
  56. z.object({
  57. peMemberId: DbSchema.act.peMembers.peMemberId,
  58. peId: DbSchema.act.partEntities.peId,
  59. peTypeId: DbSchema.act.partEntities.peTypeId,
  60. peTypeCode: DbSchema.act.peTypes.code,
  61. peTypeName: DbSchema.act.peTypes.name,
  62. name: DbSchema.act.partEntities.name,
  63. eventInstId: DbSchema.act.partEntities.eventInstId,
  64. ownerId: DbSchema.act.partEntities.ownerId,
  65. }),
  66. )`
  67. select
  68. pm.pe_member_id "peMemberId",
  69. pe.pe_id "peId",
  70. pt.pe_type_id "peTypeId",
  71. pt.code "peTypeCode",
  72. pt.name "peTypeName",
  73. pe.event_inst_id "eventInstId",
  74. pe.owner_id "ownerId",
  75. pe.name
  76. from
  77. act.pe_members pm
  78. left join act.part_entities pe on
  79. pe.pe_id = pm.pe_member_id
  80. left join act.pe_types pt on
  81. pt.pe_type_id = pe.pe_type_id
  82. where
  83. pe.pe_id = ${peId}
  84. `);
  85. }
  86. async getInvites(peId: string) {
  87. return await selPool.any(sql.type(
  88. z.object({
  89. peInviteId: DbSchema.act.peInvites.peInviteId,
  90. peInviteUuid: DbSchema.act.peInvites.peInviteUuid,
  91. name: DbSchema.act.peInvites.name,
  92. limitVal: DbSchema.act.peInvites.limitVal,
  93. countVal: DbSchema.act.peInvites.countVal,
  94. expirationDate: DbSchema.act.peInvites.expirationDate,
  95. }),
  96. )`
  97. select
  98. pe_invite_id "peInviteId",
  99. pe_invite_uuid "peInviteUuid",
  100. name,
  101. limit_val "limitVal",
  102. count_val "countVal",
  103. expiration_date "expirationDate"
  104. from
  105. act.pe_invites
  106. where
  107. pe_id = ${peId}
  108. `);
  109. }
  110. async getMembers(peId: string) {
  111. return await selPool.any(sql.type(
  112. z.object({
  113. peMemberId: z.string().uuid(),
  114. userId: z.string().uuid(),
  115. email: z.string().email(),
  116. fields: z.array(
  117. CustomFieldWithValue.extend({
  118. userEfId: z.string().uuid(),
  119. }),
  120. ),
  121. }),
  122. )`
  123. select
  124. pe_member_id "peMemberId",
  125. user_id "userId",
  126. email,
  127. fields
  128. from
  129. act.pe_members_with_fields_and_values
  130. where
  131. pe_id = ${peId}
  132. `);
  133. }
  134. async getInviteInfo(peInviteUuid: string) {
  135. return await selPool.maybeOne(sql.type(
  136. z.object({
  137. peInviteId: DbSchema.act.peInvites.peInviteId,
  138. peInviteUuid: DbSchema.act.peInvites.peInviteUuid,
  139. peId: DbSchema.act.peInvites.peId,
  140. peName: DbSchema.act.partEntities.name,
  141. peOwnerId: DbSchema.act.partEntities.ownerId,
  142. limitVal: DbSchema.act.peInvites.limitVal,
  143. countVal: DbSchema.act.peInvites.countVal,
  144. }),
  145. )`
  146. select
  147. i.pe_invite_id "peInviteId",
  148. i.pe_invite_uuid "peInviteUuid",
  149. i.pe_id "peId",
  150. pe."name" "peName",
  151. pe.owner_id "peOwnerId",
  152. i.limit_val "limitVal",
  153. i.count_val "countVal"
  154. from
  155. act.pe_invites i
  156. join act.part_entities pe on
  157. pe.pe_id = i.pe_id
  158. where
  159. pe_invite_uuid = ${peInviteUuid}
  160. `);
  161. }
  162. async getPeTypeWithFieldsAndUserCopyValues(
  163. userId: string,
  164. peTypeCode: string,
  165. eventId: string,
  166. ) {
  167. return await selPool.maybeOne(sql.type(
  168. z.object({
  169. peTypeId: DbSchema.act.peTypes.peTypeId,
  170. code: DbSchema.act.peTypes.code,
  171. name: DbSchema.act.peTypes.name,
  172. eventInstId: DbSchema.act.peTypes.eventInstId,
  173. fields: z.array(
  174. CustomFieldWithUserCopyValue.extend({ peFfId: z.string() }),
  175. ),
  176. }),
  177. )`
  178. select
  179. pt.pe_type_id as "peTypeId",
  180. pt.code,
  181. pt.name,
  182. pt.event_inst_id as "eventInstId",
  183. coalesce(jsonb_agg(jsonb_build_object(
  184. 'fieldDefinitionId',
  185. cfwv.field_definition_id,
  186. 'peFfId',
  187. pff.pe_ff_id,
  188. 'isCopyUserValue',
  189. pff.is_copy_user_value,
  190. 'code',
  191. cfwv.code,
  192. 'userCopyValue',
  193. ufwv.value,
  194. 'fieldTypeCode',
  195. cfwv.field_type_code,
  196. 'title',
  197. COALESCE(pff.field_title_override, cfwv.title),
  198. 'mask',
  199. cfwv.mask,
  200. 'options',
  201. cfwv.options,
  202. 'validators',
  203. cfwv.validators
  204. )) filter (
  205. where
  206. cfwv.field_definition_id is not null),
  207. '[]'::jsonb) as fields
  208. from
  209. act.pe_types pt
  210. -- необходимые поля
  211. left join act.pe_form_fields pff on
  212. pff.pe_type_id = pt.pe_type_id
  213. -- значение из профиля юзера
  214. left join ev.user_fields_with_values ufwv on
  215. pff.field_definition_id = ufwv.field_definition_id
  216. and ufwv.user_id = ${userId}
  217. and ufwv.event_id = ${eventId}
  218. and ufwv.value is not null
  219. -- только если нужно копировать
  220. and pff.is_copy_user_value = true
  221. left join cf.custom_fields_with_validators cfwv on
  222. pff.field_definition_id = cfwv.field_definition_id
  223. where
  224. pt.code = ${peTypeCode}
  225. group by
  226. pt.pe_type_id,
  227. pt.code,
  228. pt.name,
  229. pt.event_inst_id
  230. `);
  231. }
  232. async getPeTypeWithFields(peTypeCode: string) {
  233. return await selPool.maybeOne(sql.type(
  234. z.object({
  235. peTypeId: DbSchema.act.peTypes.peTypeId,
  236. code: DbSchema.act.peTypes.code,
  237. name: DbSchema.act.peTypes.name,
  238. eventInstId: DbSchema.act.peTypes.eventInstId,
  239. fields: z.array(
  240. CustomFieldWithValidators.extend({ peFfId: z.string() }),
  241. ),
  242. }),
  243. )`
  244. select
  245. pt.pe_type_id as "peTypeId",
  246. pt.code,
  247. pt.name,
  248. pt.event_inst_id as "eventInstId",
  249. coalesce(jsonb_agg(jsonb_build_object(
  250. 'fieldDefinitionId',
  251. cfwv.field_definition_id,
  252. 'peFfId',
  253. pff.pe_ff_id,
  254. 'code',
  255. cfwv.code,
  256. 'fieldTypeCode',
  257. cfwv.field_type_code,
  258. 'title',
  259. COALESCE(pff.field_title_override, cfwv.title),
  260. 'mask',
  261. cfwv.mask,
  262. 'options',
  263. cfwv.options,
  264. 'validators',
  265. cfwv.validators
  266. )) filter (
  267. where
  268. cfwv.field_definition_id is not null),
  269. '[]'::jsonb) as fields
  270. from
  271. act.pe_types pt
  272. -- необходимые поля
  273. left join act.pe_form_fields pff on
  274. pff.pe_type_id = pt.pe_type_id
  275. left join cf.custom_fields_with_validators cfwv on
  276. pff.field_definition_id = cfwv.field_definition_id
  277. where
  278. pt.code = ${peTypeCode}
  279. group by
  280. pt.pe_type_id,
  281. pt.code,
  282. pt.name,
  283. pt.event_inst_id
  284. `);
  285. }
  286. }
  287. export const cPeService = new CPeService();