wip: currently rewriting the project as a full stack application
tangled.org/kacaii.dev/sigo
gleam
1//// This module contains the code to run the sql queries defined in
2//// `./src/app/domain/occurrence/sql`.
3//// > 🐿️ This module was generated automatically using v4.6.0 of
4//// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
5////
6
7import gleam/dynamic/decode
8import gleam/option.{type Option}
9import gleam/time/timestamp.{type Timestamp}
10import pog
11import youid/uuid.{type Uuid}
12
13/// A row you get from running the `assign_brigades_to_occurrence` query
14/// defined in `./src/app/domain/occurrence/sql/assign_brigades_to_occurrence.sql`.
15///
16/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
17/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
18///
19pub type AssignBrigadesToOccurrenceRow {
20 AssignBrigadesToOccurrenceRow(inserted_brigade_id: Uuid)
21}
22
23/// Assign as list of brigades as participants of a occurrence
24///
25/// > 🐿️ This function was generated automatically using v4.6.0 of
26/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
27///
28pub fn assign_brigades_to_occurrence(
29 db: pog.Connection,
30 arg_1: Uuid,
31 arg_2: List(Uuid),
32) -> Result(pog.Returned(AssignBrigadesToOccurrenceRow), pog.QueryError) {
33 let decoder = {
34 use inserted_brigade_id <- decode.field(0, uuid_decoder())
35 decode.success(AssignBrigadesToOccurrenceRow(inserted_brigade_id:))
36 }
37
38 "-- Assign as list of brigades as participants of a occurrence
39select ob.inserted_brigade_id
40from public.assign_occurrence_brigades($1, $2) as ob;
41"
42 |> pog.query
43 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
44 |> pog.parameter(pog.array(
45 fn(value) { pog.text(uuid.to_string(value)) },
46 arg_2,
47 ))
48 |> pog.returning(decoder)
49 |> pog.execute(db)
50}
51
52/// A row you get from running the `close_occurrence` query
53/// defined in `./src/app/domain/occurrence/sql/close_occurrence.sql`.
54///
55/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
56/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
57///
58pub type CloseOccurrenceRow {
59 CloseOccurrenceRow(
60 id: Uuid,
61 resolved_at: Option(Timestamp),
62 updated_at: Timestamp,
63 )
64}
65
66/// Mark a occurrence as resolved
67///
68/// > 🐿️ This function was generated automatically using v4.6.0 of
69/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
70///
71pub fn close_occurrence(
72 db: pog.Connection,
73 arg_1: Uuid,
74) -> Result(pog.Returned(CloseOccurrenceRow), pog.QueryError) {
75 let decoder = {
76 use id <- decode.field(0, uuid_decoder())
77 use resolved_at <- decode.field(1, decode.optional(pog.timestamp_decoder()))
78 use updated_at <- decode.field(2, pog.timestamp_decoder())
79 decode.success(CloseOccurrenceRow(id:, resolved_at:, updated_at:))
80 }
81
82 "-- Mark a occurrence as resolved
83update public.occurrence
84set
85 resolved_at = current_timestamp,
86 updated_at = current_timestamp
87where id = $1
88returning
89 id,
90 resolved_at,
91 updated_at;
92"
93 |> pog.query
94 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
95 |> pog.returning(decoder)
96 |> pog.execute(db)
97}
98
99/// A row you get from running the `delete_occurrence_by_id` query
100/// defined in `./src/app/domain/occurrence/sql/delete_occurrence_by_id.sql`.
101///
102/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
103/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
104///
105pub type DeleteOccurrenceByIdRow {
106 DeleteOccurrenceByIdRow(id: Uuid)
107}
108
109/// Remove an occurrence from the database
110///
111/// > 🐿️ This function was generated automatically using v4.6.0 of
112/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
113///
114pub fn delete_occurrence_by_id(
115 db: pog.Connection,
116 arg_1: Uuid,
117) -> Result(pog.Returned(DeleteOccurrenceByIdRow), pog.QueryError) {
118 let decoder = {
119 use id <- decode.field(0, uuid_decoder())
120 decode.success(DeleteOccurrenceByIdRow(id:))
121 }
122
123 "-- Remove an occurrence from the database
124delete from public.occurrence as o
125where o.id = $1
126returning o.id;
127"
128 |> pog.query
129 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
130 |> pog.returning(decoder)
131 |> pog.execute(db)
132}
133
134/// A row you get from running the `insert_new_occurence` query
135/// defined in `./src/app/domain/occurrence/sql/insert_new_occurence.sql`.
136///
137/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
138/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
139///
140pub type InsertNewOccurenceRow {
141 InsertNewOccurenceRow(
142 id: Uuid,
143 occurrence_category: OccurrenceCategoryEnum,
144 priority: OccurrencePriorityEnum,
145 applicant_id: Uuid,
146 created_at: Timestamp,
147 )
148}
149
150/// Inserts a new occurrence into the database
151///
152/// > 🐿️ This function was generated automatically using v4.6.0 of
153/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
154///
155pub fn insert_new_occurence(
156 db: pog.Connection,
157 arg_1: Uuid,
158 arg_2: OccurrenceCategoryEnum,
159 arg_3: OccurrenceSubcategoryEnum,
160 arg_4: OccurrencePriorityEnum,
161 arg_5: String,
162 arg_6: List(Float),
163 arg_7: String,
164) -> Result(pog.Returned(InsertNewOccurenceRow), pog.QueryError) {
165 let decoder = {
166 use id <- decode.field(0, uuid_decoder())
167 use occurrence_category <- decode.field(
168 1,
169 occurrence_category_enum_decoder(),
170 )
171 use priority <- decode.field(2, occurrence_priority_enum_decoder())
172 use applicant_id <- decode.field(3, uuid_decoder())
173 use created_at <- decode.field(4, pog.timestamp_decoder())
174 decode.success(InsertNewOccurenceRow(
175 id:,
176 occurrence_category:,
177 priority:,
178 applicant_id:,
179 created_at:,
180 ))
181 }
182
183 "-- Inserts a new occurrence into the database
184insert into public.occurrence as o (
185 applicant_id,
186 occurrence_category,
187 occurrence_subcategory,
188 priority,
189 description,
190 occurrence_location,
191 reference_point
192) values (
193 $1,
194 $2,
195 $3,
196 $4,
197 $5,
198 $6,
199 $7
200)
201returning
202 o.id,
203 o.occurrence_category,
204 o.priority,
205 o.applicant_id,
206 o.created_at;
207"
208 |> pog.query
209 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
210 |> pog.parameter(occurrence_category_enum_encoder(arg_2))
211 |> pog.parameter(occurrence_subcategory_enum_encoder(arg_3))
212 |> pog.parameter(occurrence_priority_enum_encoder(arg_4))
213 |> pog.parameter(pog.text(arg_5))
214 |> pog.parameter(pog.array(fn(value) { pog.float(value) }, arg_6))
215 |> pog.parameter(pog.text(arg_7))
216 |> pog.returning(decoder)
217 |> pog.execute(db)
218}
219
220/// A row you get from running the `query_occurences_by_applicant` query
221/// defined in `./src/app/domain/occurrence/sql/query_occurences_by_applicant.sql`.
222///
223/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
224/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
225///
226pub type QueryOccurencesByApplicantRow {
227 QueryOccurencesByApplicantRow(
228 id: Uuid,
229 resolved_at: Option(Timestamp),
230 priority: OccurrencePriorityEnum,
231 occurrence_category: OccurrenceCategoryEnum,
232 occurrence_location: Option(List(Float)),
233 details: Option(String),
234 applicant_name: String,
235 created_at: Timestamp,
236 arrived_at: Option(Timestamp),
237 applicant_registration: String,
238 applicant_id: Uuid,
239 brigade_list: String,
240 )
241}
242
243/// Retrieves all occurrences associated with a user,
244/// including detailed category information and resolution status.
245///
246/// > 🐿️ This function was generated automatically using v4.6.0 of
247/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
248///
249pub fn query_occurences_by_applicant(
250 db: pog.Connection,
251 arg_1: Uuid,
252) -> Result(pog.Returned(QueryOccurencesByApplicantRow), pog.QueryError) {
253 let decoder = {
254 use id <- decode.field(0, uuid_decoder())
255 use resolved_at <- decode.field(1, decode.optional(pog.timestamp_decoder()))
256 use priority <- decode.field(2, occurrence_priority_enum_decoder())
257 use occurrence_category <- decode.field(
258 3,
259 occurrence_category_enum_decoder(),
260 )
261 use occurrence_location <- decode.field(
262 4,
263 decode.optional(decode.list(decode.float)),
264 )
265 use details <- decode.field(5, decode.optional(decode.string))
266 use applicant_name <- decode.field(6, decode.string)
267 use created_at <- decode.field(7, pog.timestamp_decoder())
268 use arrived_at <- decode.field(8, decode.optional(pog.timestamp_decoder()))
269 use applicant_registration <- decode.field(9, decode.string)
270 use applicant_id <- decode.field(10, uuid_decoder())
271 use brigade_list <- decode.field(11, decode.string)
272 decode.success(QueryOccurencesByApplicantRow(
273 id:,
274 resolved_at:,
275 priority:,
276 occurrence_category:,
277 occurrence_location:,
278 details:,
279 applicant_name:,
280 created_at:,
281 arrived_at:,
282 applicant_registration:,
283 applicant_id:,
284 brigade_list:,
285 ))
286 }
287
288 "-- Retrieves all occurrences associated with a user,
289-- including detailed category information and resolution status.
290select
291 o.id,
292 o.resolved_at,
293 o.priority,
294 o.occurrence_category,
295 o.occurrence_location,
296 o.description as details,
297 u.full_name as applicant_name,
298 o.created_at,
299 o.arrived_at,
300 u.registration as applicant_registration,
301 o.applicant_id,
302
303 (
304 select json_agg(json_build_object(
305 'id', b.id,
306 'brigade_name', b.brigade_name,
307 'leader_full_name', leader_u.full_name,
308 'vehicle_code', b.vehicle_code
309 )) from public.occurrence_brigade as ob
310 inner join public.brigade as b
311 on ob.brigade_id = b.id
312 inner join public.user_account as leader_u
313 on b.leader_id = leader_u.id
314 where ob.occurrence_id = o.id
315 ) as brigade_list
316
317from public.occurrence as o
318inner join public.user_account as u
319 on o.applicant_id = u.id
320where o.applicant_id = $1;
321"
322 |> pog.query
323 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
324 |> pog.returning(decoder)
325 |> pog.execute(db)
326}
327
328/// A row you get from running the `query_participants` query
329/// defined in `./src/app/domain/occurrence/sql/query_participants.sql`.
330///
331/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
332/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
333///
334pub type QueryParticipantsRow {
335 QueryParticipantsRow(user_id: Uuid)
336}
337
338/// Find all users that participated in a occurrence
339///
340/// > 🐿️ This function was generated automatically using v4.6.0 of
341/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
342///
343pub fn query_participants(
344 db: pog.Connection,
345 arg_1: Uuid,
346) -> Result(pog.Returned(QueryParticipantsRow), pog.QueryError) {
347 let decoder = {
348 use user_id <- decode.field(0, uuid_decoder())
349 decode.success(QueryParticipantsRow(user_id:))
350 }
351
352 "-- Find all users that participated in a occurrence
353select distinct participant.user_id
354from public.brigade_membership as participant
355inner join public.occurrence_brigade as ob
356 on participant.brigade_id = ob.brigade_id
357where ob.occurrence_id = $1
358order by participant.user_id;
359"
360 |> pog.query
361 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
362 |> pog.returning(decoder)
363 |> pog.execute(db)
364}
365
366/// A row you get from running the `query_recent_occurrences` query
367/// defined in `./src/app/domain/occurrence/sql/query_recent_occurrences.sql`.
368///
369/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
370/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
371///
372pub type QueryRecentOccurrencesRow {
373 QueryRecentOccurrencesRow(
374 id: Uuid,
375 created_at: Timestamp,
376 description: Option(String),
377 occurrence_category: OccurrenceCategoryEnum,
378 occurrence_subcategory: Option(OccurrenceSubcategoryEnum),
379 occurrence_location: Option(List(Float)),
380 reference_point: Option(String),
381 )
382}
383
384/// Find all occurrences from the last 24 hours
385///
386/// > 🐿️ This function was generated automatically using v4.6.0 of
387/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
388///
389pub fn query_recent_occurrences(
390 db: pog.Connection,
391) -> Result(pog.Returned(QueryRecentOccurrencesRow), pog.QueryError) {
392 let decoder = {
393 use id <- decode.field(0, uuid_decoder())
394 use created_at <- decode.field(1, pog.timestamp_decoder())
395 use description <- decode.field(2, decode.optional(decode.string))
396 use occurrence_category <- decode.field(
397 3,
398 occurrence_category_enum_decoder(),
399 )
400 use occurrence_subcategory <- decode.field(
401 4,
402 decode.optional(occurrence_subcategory_enum_decoder()),
403 )
404 use occurrence_location <- decode.field(
405 5,
406 decode.optional(decode.list(decode.float)),
407 )
408 use reference_point <- decode.field(6, decode.optional(decode.string))
409 decode.success(QueryRecentOccurrencesRow(
410 id:,
411 created_at:,
412 description:,
413 occurrence_category:,
414 occurrence_subcategory:,
415 occurrence_location:,
416 reference_point:,
417 ))
418 }
419
420 "-- Find all occurrences from the last 24 hours
421select
422 o.id,
423 o.created_at,
424 o.description,
425 o.occurrence_category,
426 o.occurrence_subcategory,
427 o.occurrence_location,
428 o.reference_point
429from public.occurrence as o
430where o.created_at >= (now() - '1 day'::interval);
431"
432 |> pog.query
433 |> pog.returning(decoder)
434 |> pog.execute(db)
435}
436
437/// A row you get from running the `reopen_occurrence` query
438/// defined in `./src/app/domain/occurrence/sql/reopen_occurrence.sql`.
439///
440/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
441/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
442///
443pub type ReopenOccurrenceRow {
444 ReopenOccurrenceRow(
445 id: Uuid,
446 resolved_at: Option(Timestamp),
447 updated_at: Timestamp,
448 )
449}
450
451/// Mark a occurrence as unresolved
452///
453/// > 🐿️ This function was generated automatically using v4.6.0 of
454/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
455///
456pub fn reopen_occurrence(
457 db: pog.Connection,
458 arg_1: Uuid,
459) -> Result(pog.Returned(ReopenOccurrenceRow), pog.QueryError) {
460 let decoder = {
461 use id <- decode.field(0, uuid_decoder())
462 use resolved_at <- decode.field(1, decode.optional(pog.timestamp_decoder()))
463 use updated_at <- decode.field(2, pog.timestamp_decoder())
464 decode.success(ReopenOccurrenceRow(id:, resolved_at:, updated_at:))
465 }
466
467 "-- Mark a occurrence as unresolved
468update public.occurrence
469set
470 resolved_at = null,
471 updated_at = current_timestamp
472where id = $1
473returning
474 id,
475 resolved_at,
476 updated_at;
477"
478 |> pog.query
479 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
480 |> pog.returning(decoder)
481 |> pog.execute(db)
482}
483
484/// A row you get from running the `replace_occurrence_brigades` query
485/// defined in `./src/app/domain/occurrence/sql/replace_occurrence_brigades.sql`.
486///
487/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
488/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
489///
490pub type ReplaceOccurrenceBrigadesRow {
491 ReplaceOccurrenceBrigadesRow(inserted_brigade_id: Uuid)
492}
493
494/// Replace all assigned brigades
495///
496/// > 🐿️ This function was generated automatically using v4.6.0 of
497/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
498///
499pub fn replace_occurrence_brigades(
500 db: pog.Connection,
501 arg_1: Uuid,
502 arg_2: List(Uuid),
503) -> Result(pog.Returned(ReplaceOccurrenceBrigadesRow), pog.QueryError) {
504 let decoder = {
505 use inserted_brigade_id <- decode.field(0, uuid_decoder())
506 decode.success(ReplaceOccurrenceBrigadesRow(inserted_brigade_id:))
507 }
508
509 "-- Replace all assigned brigades
510select o.inserted_brigade_id
511from public.assign_occurrence_brigades($1, $2) as o;
512"
513 |> pog.query
514 |> pog.parameter(pog.text(uuid.to_string(arg_1)))
515 |> pog.parameter(pog.array(
516 fn(value) { pog.text(uuid.to_string(value)) },
517 arg_2,
518 ))
519 |> pog.returning(decoder)
520 |> pog.execute(db)
521}
522
523// --- Enums -------------------------------------------------------------------
524
525/// Corresponds to the Postgres `occurrence_category_enum` enum.
526///
527/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
528/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
529///
530pub type OccurrenceCategoryEnum {
531 Other
532 TrafficAccident
533 Fire
534 MedicEmergency
535}
536
537fn occurrence_category_enum_decoder() -> decode.Decoder(OccurrenceCategoryEnum) {
538 use occurrence_category_enum <- decode.then(decode.string)
539 case occurrence_category_enum {
540 "other" -> decode.success(Other)
541 "traffic_accident" -> decode.success(TrafficAccident)
542 "fire" -> decode.success(Fire)
543 "medic_emergency" -> decode.success(MedicEmergency)
544 _ -> decode.failure(Other, "OccurrenceCategoryEnum")
545 }
546}
547
548fn occurrence_category_enum_encoder(occurrence_category_enum) -> pog.Value {
549 case occurrence_category_enum {
550 Other -> "other"
551 TrafficAccident -> "traffic_accident"
552 Fire -> "fire"
553 MedicEmergency -> "medic_emergency"
554 }
555 |> pog.text
556}
557
558/// Corresponds to the Postgres `occurrence_priority_enum` enum.
559///
560/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
561/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
562///
563pub type OccurrencePriorityEnum {
564 High
565 Medium
566 Low
567}
568
569fn occurrence_priority_enum_decoder() -> decode.Decoder(OccurrencePriorityEnum) {
570 use occurrence_priority_enum <- decode.then(decode.string)
571 case occurrence_priority_enum {
572 "high" -> decode.success(High)
573 "medium" -> decode.success(Medium)
574 "low" -> decode.success(Low)
575 _ -> decode.failure(High, "OccurrencePriorityEnum")
576 }
577}
578
579fn occurrence_priority_enum_encoder(occurrence_priority_enum) -> pog.Value {
580 case occurrence_priority_enum {
581 High -> "high"
582 Medium -> "medium"
583 Low -> "low"
584 }
585 |> pog.text
586}
587
588/// Corresponds to the Postgres `occurrence_subcategory_enum` enum.
589///
590/// > 🐿️ This type definition was generated automatically using v4.6.0 of the
591/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
592///
593pub type OccurrenceSubcategoryEnum {
594 InjuredAnimal
595 Flood
596 TreeCrash
597 MotorcycleCrash
598 Rollover
599 RunOver
600 Collision
601 Vehicle
602 Vegetation
603 Comercial
604 Residential
605 Intoxication
606 SeriousInjury
607 Seizure
608 PreHospitalCare
609 HeartStop
610}
611
612fn occurrence_subcategory_enum_decoder() -> decode.Decoder(
613 OccurrenceSubcategoryEnum,
614) {
615 use occurrence_subcategory_enum <- decode.then(decode.string)
616 case occurrence_subcategory_enum {
617 "injured_animal" -> decode.success(InjuredAnimal)
618 "flood" -> decode.success(Flood)
619 "tree_crash" -> decode.success(TreeCrash)
620 "motorcycle_crash" -> decode.success(MotorcycleCrash)
621 "rollover" -> decode.success(Rollover)
622 "run_over" -> decode.success(RunOver)
623 "collision" -> decode.success(Collision)
624 "vehicle" -> decode.success(Vehicle)
625 "vegetation" -> decode.success(Vegetation)
626 "comercial" -> decode.success(Comercial)
627 "residential" -> decode.success(Residential)
628 "intoxication" -> decode.success(Intoxication)
629 "serious_injury" -> decode.success(SeriousInjury)
630 "seizure" -> decode.success(Seizure)
631 "pre_hospital_care" -> decode.success(PreHospitalCare)
632 "heart_stop" -> decode.success(HeartStop)
633 _ -> decode.failure(InjuredAnimal, "OccurrenceSubcategoryEnum")
634 }
635}
636
637fn occurrence_subcategory_enum_encoder(occurrence_subcategory_enum) -> pog.Value {
638 case occurrence_subcategory_enum {
639 InjuredAnimal -> "injured_animal"
640 Flood -> "flood"
641 TreeCrash -> "tree_crash"
642 MotorcycleCrash -> "motorcycle_crash"
643 Rollover -> "rollover"
644 RunOver -> "run_over"
645 Collision -> "collision"
646 Vehicle -> "vehicle"
647 Vegetation -> "vegetation"
648 Comercial -> "comercial"
649 Residential -> "residential"
650 Intoxication -> "intoxication"
651 SeriousInjury -> "serious_injury"
652 Seizure -> "seizure"
653 PreHospitalCare -> "pre_hospital_care"
654 HeartStop -> "heart_stop"
655 }
656 |> pog.text
657}
658
659// --- Encoding/decoding utils -------------------------------------------------
660
661/// A decoder to decode `Uuid`s coming from a Postgres query.
662///
663fn uuid_decoder() {
664 use bit_array <- decode.then(decode.bit_array)
665 case uuid.from_bit_array(bit_array) {
666 Ok(uuid) -> decode.success(uuid)
667 Error(_) -> decode.failure(uuid.v7(), "Uuid")
668 }
669}