lariv-rs 0.1.0

Compile-time plugin web application framework built on Axum, SeaORM, Maud, and HTMX
Documentation
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[derive(DeriveIden)]
enum CrmLeads {
    Table,
    Id,
    CreatedAt,
    UpdatedAt,
    CompanyName,
    FirstName,
    LastName,
    Email,
    Phone,
    Source,
    Notes,
}

#[derive(DeriveIden)]
enum CrmCompanies {
    Table,
    Id,
    CreatedAt,
    UpdatedAt,
    Name,
    City,
    Pincode,
    State,
    Website,
}

#[derive(DeriveIden)]
enum CrmContacts {
    Table,
    Id,
    CreatedAt,
    UpdatedAt,
    CompanyId,
    FirstName,
    LastName,
    Email,
    Phone,
    Title,
    IsPrimary,
}

#[derive(DeriveIden)]
enum CrmDeals {
    Table,
    Id,
    CreatedAt,
    UpdatedAt,
    CompanyId,
    PrimaryContactId,
    Name,
    Amount,
    Stage,
    ExpectedCloseDate,
}

#[derive(DeriveIden)]
enum CrmConvertedLeads {
    Table,
    Id,
    CreatedAt,
    LeadId,
    ConvertedAt,
    CompanyId,
    ContactId,
    DealId,
}

#[derive(DeriveIden)]
enum CrmFailedLeads {
    Table,
    Id,
    CreatedAt,
    LeadId,
    FailedAt,
    Reason,
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(CrmLeads::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(CrmLeads::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(CrmLeads::CreatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(CrmLeads::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(CrmLeads::CompanyName).text())
                    .col(ColumnDef::new(CrmLeads::FirstName).text())
                    .col(ColumnDef::new(CrmLeads::LastName).text())
                    .col(ColumnDef::new(CrmLeads::Email).text())
                    .col(ColumnDef::new(CrmLeads::Phone).text())
                    .col(
                        ColumnDef::new(CrmLeads::Source)
                            .string_len(32)
                            .not_null()
                            .default("web_form"),
                    )
                    .col(ColumnDef::new(CrmLeads::Notes).text())
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(CrmCompanies::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(CrmCompanies::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(CrmCompanies::CreatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(CrmCompanies::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(CrmCompanies::Name).text().not_null())
                    .col(ColumnDef::new(Alias::new("address_line_1")).text())
                    .col(ColumnDef::new(Alias::new("address_line_2")).text())
                    .col(ColumnDef::new(CrmCompanies::City).text())
                    .col(ColumnDef::new(CrmCompanies::Pincode).text())
                    .col(ColumnDef::new(CrmCompanies::State).text())
                    .col(ColumnDef::new(CrmCompanies::Website).text())
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(CrmContacts::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(CrmContacts::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(CrmContacts::CreatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(CrmContacts::UpdatedAt).timestamp_with_time_zone())
                    .col(
                        ColumnDef::new(CrmContacts::CompanyId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(ColumnDef::new(CrmContacts::FirstName).text().not_null())
                    .col(ColumnDef::new(CrmContacts::LastName).text())
                    .col(ColumnDef::new(CrmContacts::Email).text())
                    .col(ColumnDef::new(CrmContacts::Phone).text())
                    .col(ColumnDef::new(CrmContacts::Title).text())
                    .col(
                        ColumnDef::new(CrmContacts::IsPrimary)
                            .boolean()
                            .not_null()
                            .default(false),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_contacts_company_id")
                            .from(CrmContacts::Table, CrmContacts::CompanyId)
                            .to(CrmCompanies::Table, CrmCompanies::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(CrmDeals::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(CrmDeals::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(CrmDeals::CreatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(CrmDeals::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(CrmDeals::CompanyId).big_integer().not_null())
                    .col(
                        ColumnDef::new(CrmDeals::PrimaryContactId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(ColumnDef::new(CrmDeals::Name).text().not_null())
                    .col(ColumnDef::new(CrmDeals::Amount).decimal_len(19, 4))
                    .col(
                        ColumnDef::new(CrmDeals::Stage)
                            .string_len(32)
                            .not_null()
                            .default("prospecting"),
                    )
                    .col(ColumnDef::new(CrmDeals::ExpectedCloseDate).date())
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_deals_company_id")
                            .from(CrmDeals::Table, CrmDeals::CompanyId)
                            .to(CrmCompanies::Table, CrmCompanies::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_deals_primary_contact_id")
                            .from(CrmDeals::Table, CrmDeals::PrimaryContactId)
                            .to(CrmContacts::Table, CrmContacts::Id)
                            .on_delete(ForeignKeyAction::Restrict)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(CrmConvertedLeads::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(CrmConvertedLeads::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(CrmConvertedLeads::CreatedAt).timestamp_with_time_zone())
                    .col(
                        ColumnDef::new(CrmConvertedLeads::LeadId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(CrmConvertedLeads::ConvertedAt)
                            .timestamp_with_time_zone()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(CrmConvertedLeads::CompanyId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(CrmConvertedLeads::ContactId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(ColumnDef::new(CrmConvertedLeads::DealId).big_integer())
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_converted_leads_lead_id")
                            .from(CrmConvertedLeads::Table, CrmConvertedLeads::LeadId)
                            .to(CrmLeads::Table, CrmLeads::Id)
                            .on_delete(ForeignKeyAction::Restrict)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_converted_leads_company_id")
                            .from(CrmConvertedLeads::Table, CrmConvertedLeads::CompanyId)
                            .to(CrmCompanies::Table, CrmCompanies::Id)
                            .on_delete(ForeignKeyAction::Restrict)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_converted_leads_contact_id")
                            .from(CrmConvertedLeads::Table, CrmConvertedLeads::ContactId)
                            .to(CrmContacts::Table, CrmContacts::Id)
                            .on_delete(ForeignKeyAction::Restrict)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_converted_leads_deal_id")
                            .from(CrmConvertedLeads::Table, CrmConvertedLeads::DealId)
                            .to(CrmDeals::Table, CrmDeals::Id)
                            .on_delete(ForeignKeyAction::SetNull)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(CrmFailedLeads::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(CrmFailedLeads::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(CrmFailedLeads::CreatedAt).timestamp_with_time_zone())
                    .col(
                        ColumnDef::new(CrmFailedLeads::LeadId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(CrmFailedLeads::FailedAt)
                            .timestamp_with_time_zone()
                            .not_null(),
                    )
                    .col(ColumnDef::new(CrmFailedLeads::Reason).text())
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_crm_failed_leads_lead_id")
                            .from(CrmFailedLeads::Table, CrmFailedLeads::LeadId)
                            .to(CrmLeads::Table, CrmLeads::Id)
                            .on_delete(ForeignKeyAction::Restrict)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .if_not_exists()
                    .name("uix_crm_converted_leads_lead_id")
                    .table(CrmConvertedLeads::Table)
                    .col(CrmConvertedLeads::LeadId)
                    .unique()
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .if_not_exists()
                    .name("uix_crm_failed_leads_lead_id")
                    .table(CrmFailedLeads::Table)
                    .col(CrmFailedLeads::LeadId)
                    .unique()
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .if_not_exists()
                    .name("idx_crm_contacts_company_id")
                    .table(CrmContacts::Table)
                    .col(CrmContacts::CompanyId)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .if_not_exists()
                    .name("idx_crm_deals_company_id")
                    .table(CrmDeals::Table)
                    .col(CrmDeals::CompanyId)
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(CrmFailedLeads::Table).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(CrmConvertedLeads::Table).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(CrmDeals::Table).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(CrmContacts::Table).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(CrmCompanies::Table).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(CrmLeads::Table).to_owned())
            .await
    }
}