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
//! The models the documentation examples use (`Deal`, `Company`,
//! `Activity`, `Tag`, `Note`, `Setting`, `Event`) and their relations, with
//! every name the examples need in scope, so that each example compiles.
//! Not API: hidden from the docs, and it may change at any time.
pub use chrono::{DateTime, Duration, Utc};
pub use indexmap::IndexMap;
pub use navree_db::{Db, PageQuery, Uow};
pub use uuid::Uuid;
pub use crate::ir::FullText;
pub use crate::prelude::*;
pub use crate::{Page, Result};
use serde::{Deserialize, Serialize};
/// A deal's stage, stored as its name.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Stage {
/// Being worked on.
#[default]
Open,
/// Closed and won.
Won,
/// Closed and lost.
Lost,
}
/// A sales deal.
#[derive(Model, Clone, Debug, Default, Serialize)]
#[model(timestamps)]
pub struct Deal {
/// The key (a UUID v7, generated on insert).
pub id: Uuid,
/// The company the deal is with.
pub company_id: Option<i64>,
/// The user who owns the deal.
pub owner_id: Option<i64>,
/// The title.
pub title: String,
/// Free-text notes.
pub notes: Option<String>,
/// The stage.
#[model(cast = "string_enum")]
pub stage: Stage,
/// The value, in cents.
pub amount_cents: i64,
/// How often the deal was viewed.
pub views: i64,
/// When the deal closed.
pub closed_at: Option<DateTime<Utc>>,
/// When the deal was created.
pub created_at: DateTime<Utc>,
/// When the deal last changed.
pub updated_at: DateTime<Utc>,
}
#[relations]
impl Deal {
/// The company the deal is with.
pub fn company(&self) -> BelongsTo<Deal, Company> {
self.belongs_to(Deal::COMPANY_ID)
}
/// The deal's activities.
pub fn activities(&self) -> HasMany<Deal, Activity> {
self.has_many(Activity::DEAL_ID)
}
/// The deal's latest activity.
pub fn latest_activity(&self) -> HasOne<Deal, Activity> {
self.has_many(Activity::DEAL_ID).latest_of_many()
}
/// The deal's tags, through `deal_tag`.
pub fn tags(&self) -> BelongsToMany<Deal, Tag> {
self.belongs_to_many("deal_tag")
.with_pivot(["role"])
.with_timestamps()
}
/// The notes on the deal.
pub fn notes(&self) -> MorphMany<Deal, Note> {
self.morph_many("notable")
}
}
/// A customer company.
#[derive(Model, Clone, Debug, Default, Serialize)]
#[model(table = "companies")]
pub struct Company {
/// The key (assigned by the database).
pub id: i64,
/// The name.
pub name: String,
/// Whether the company is a current customer.
pub active: bool,
}
#[relations]
impl Company {
/// The company's deals.
pub fn deals(&self) -> HasMany<Company, Deal> {
self.has_many(Deal::COMPANY_ID)
}
}
/// A call, meeting or task on a deal.
#[derive(Model, Clone, Debug, Default, Serialize)]
#[model(timestamps)]
pub struct Activity {
/// The key (assigned by the database).
pub id: i64,
/// The deal the activity belongs to.
pub deal_id: Uuid,
/// What it is about.
pub subject: String,
/// When it is due.
pub due_at: DateTime<Utc>,
/// When it was created.
pub created_at: DateTime<Utc>,
/// When it last changed.
pub updated_at: DateTime<Utc>,
}
#[relations]
impl Activity {
/// The deal the activity is on.
pub fn deal(&self) -> BelongsTo<Activity, Deal> {
self.belongs_to(Activity::DEAL_ID)
}
}
/// A label on deals.
#[derive(Model, Clone, Debug, Default, Serialize)]
pub struct Tag {
/// The key (assigned by the database).
pub id: i64,
/// The name.
pub name: String,
/// The display color.
pub color: String,
}
#[relations]
impl Tag {
/// The deals with the tag.
pub fn deals(&self) -> BelongsToMany<Tag, Deal> {
self.belongs_to_many("deal_tag")
.with_pivot(["role"])
.with_timestamps()
}
}
/// A note on a deal or an event, which goes to the trash before it is
/// gone.
#[derive(Model, Clone, Debug, Default, Serialize)]
#[model(timestamps, soft_deletes)]
pub struct Note {
/// The key (assigned by the database).
pub id: i64,
/// The morph name of what the note is on.
pub notable_type: Option<String>,
/// The key of what the note is on.
pub notable_id: Option<Uuid>,
/// The text.
pub body: String,
/// When it was written.
pub created_at: DateTime<Utc>,
/// When it last changed.
pub updated_at: DateTime<Utc>,
/// When it was trashed.
pub deleted_at: Option<DateTime<Utc>>,
}
#[relations]
impl Note {
/// What the note is on: a deal or an event.
pub fn notable(&self) -> MorphTo<Note> {
self.morph_to("notable").targets::<(Deal, Event)>()
}
}
/// An application setting.
#[derive(Model, Clone, Debug, Default, Serialize)]
pub struct Setting {
/// The key (assigned by the database).
pub id: i64,
/// The setting's name.
pub key: String,
/// Its value.
pub value: String,
}
/// Something that happened, for a feed.
#[derive(Model, Clone, Debug, Default, Serialize)]
#[model(timestamps)]
pub struct Event {
/// The key (a UUID v7, generated on insert).
pub id: Uuid,
/// What happened.
pub name: String,
/// When it was recorded.
pub created_at: DateTime<Utc>,
/// When it last changed.
pub updated_at: DateTime<Utc>,
}