c-pe-service.ts 10 KB

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