1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
include Sihl.Contract.Email_template
let log_src =
Logs.Src.create ("sihl.service." ^ Sihl.Contract.Email_template.name)
;;
module Logs = (val Logs.src_log log_src : Logs.LOG)
module Make (Repo : Template_repo_sql.Sig) : Sihl.Contract.Email_template.Sig =
struct
let get = Repo.get
let get_by_label = Repo.get_by_label
let create ?ctx ?id ?html ~label text =
let open Sihl.Contract.Email_template in
let now = Ptime_clock.now () in
let id = Option.value id ~default:(Uuidm.v `V4 |> Uuidm.to_string) in
let template =
{ id; label; html; text; created_at = now; updated_at = now }
in
let%lwt () = Repo.insert ?ctx template in
let%lwt created = Repo.get ?ctx id in
match created with
| None ->
Logs.err (fun m ->
m
"Could not create template %a"
Sihl.Contract.Email_template.pp
template);
raise (Sihl.Contract.Email.Exception "Could not create email template")
| Some created -> Lwt.return created
;;
let update ?ctx template =
let%lwt () = Repo.update ?ctx template in
let id = template.id in
let%lwt created = Repo.get ?ctx id in
match created with
| None ->
Logs.err (fun m ->
m
"Could not update template %a"
Sihl.Contract.Email_template.pp
template);
raise (Sihl.Contract.Email.Exception "Could not create email template")
| Some created -> Lwt.return created
;;
let start () = Lwt.return ()
let stop () = Lwt.return ()
let lifecycle =
Sihl.Container.create_lifecycle
Sihl.Contract.Email_template.name
~dependencies:(fun () -> Repo.lifecycles)
~start
~stop
;;
let register () =
Repo.register_migration ();
Repo.register_cleaner ();
Sihl.Container.Service.create lifecycle
;;
end
module PostgreSql =
Make (Template_repo_sql.MakePostgreSql (Sihl.Database.Migration.PostgreSql))
module MariaDb =
Make (Template_repo_sql.MakeMariaDb (Sihl.Database.Migration.MariaDb))