-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcampaign.rs
More file actions
363 lines (346 loc) · 12.1 KB
/
campaign.rs
File metadata and controls
363 lines (346 loc) · 12.1 KB
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
//! Campaign handler for the Chaos application.
//!
//! This module provides HTTP request handlers for managing campaigns, including:
//! - Campaign CRUD operations
//! - Role management within campaigns
//! - Application management
//! - Offer management
//! - Banner image handling
use crate::models;
use crate::models::app::{AppMessage, AppState};
use crate::models::application::Application;
use crate::models::application::NewApplication;
use crate::models::auth::AuthUser;
use crate::models::auth::CampaignAdmin;
use crate::models::campaign::{Campaign, OpenCampaign};
use crate::models::error::ChaosError;
use crate::models::offer::Offer;
use crate::models::role::{Role, RoleUpdate};
use crate::models::transaction::DBTransaction;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
/// Handler for campaign-related HTTP requests.
pub struct CampaignHandler;
impl CampaignHandler {
/// Retrieves a campaign by its ID.
///
/// This handler allows any authenticated user to view campaign details.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `id` - The ID of the campaign to retrieve
/// * `_user` - The authenticated user
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Campaign details or error
pub async fn get(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
// no need for AuthUser as this is public
) -> Result<impl IntoResponse, ChaosError> {
let campaign = Campaign::get(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(campaign)))
}
/// Retrieves a campaign by its organisation and campaign slugs.
///
/// This handler allows any authenticated user to view campaign details using slugs.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `organisation_slug` - The slug of the organisation
/// * `campaign_slug` - The slug of the campaign
/// * `_user` - The authenticated user
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Campaign details or error
pub async fn get_by_slugs(
mut transaction: DBTransaction<'_>,
Path((organisation_slug, campaign_slug)): Path<(String, String)>,
) -> Result<impl IntoResponse, ChaosError> {
let campaign =
Campaign::get_by_slugs(organisation_slug, campaign_slug, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(campaign)))
}
/// Retrieves all campaigns.
///
/// This handler allows any authenticated user to view all campaigns.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `_user` - The authenticated user
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of campaigns or error
pub async fn get_all(
mut transaction: DBTransaction<'_>,
_user: AuthUser,
) -> Result<impl IntoResponse, ChaosError> {
let campaigns = Campaign::get_all(&mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(campaigns)))
}
/// Updates a campaign.
///
/// This handler allows campaign admins to update campaign details.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `id` - The ID of the campaign to update
/// * `_admin` - The authenticated user (must be a campaign admin)
/// * `request_body` - The new campaign details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn update(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_admin: CampaignAdmin,
Json(request_body): Json<models::campaign::CampaignUpdate>,
) -> Result<impl IntoResponse, ChaosError> {
Campaign::update(id, request_body, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully updated campaign"))
}
/// Publishes a campaign by setting its published field to true.
///
/// This handler allows campaign admins to publish campaigns.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `id` - The ID of the campaign to publish
/// * `_admin` - The authenticated user (must be a campaign admin)
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn publish(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_admin: CampaignAdmin,
) -> Result<impl IntoResponse, ChaosError> {
Campaign::publish(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully published campaign"))
}
/// Updates a campaign's banner image.
///
/// This handler allows campaign admins to update the campaign's banner image.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `state` - The application state
/// * `id` - The ID of the campaign
/// * `_admin` - The authenticated user (must be a campaign admin)
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Banner URL or error
pub async fn update_banner(
mut transaction: DBTransaction<'_>,
State(state): State<AppState>,
Path(id): Path<i64>,
_admin: CampaignAdmin,
) -> Result<impl IntoResponse, ChaosError> {
let banner_url =
Campaign::update_banner(id, &mut transaction.tx, &state.storage_bucket).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(banner_url)))
}
/// Deletes a campaign.
///
/// This handler allows campaign admins to delete campaigns.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `id` - The ID of the campaign to delete
/// * `_admin` - The authenticated user (must be a campaign admin)
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn delete(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_admin: CampaignAdmin,
) -> Result<impl IntoResponse, ChaosError> {
Campaign::delete(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully deleted campaign"))
}
/// Creates a new role in a campaign.
///
/// This handler allows campaign admins to create new roles.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `state` - The application state
/// * `id` - The ID of the campaign
/// * `_admin` - The authenticated user (must be a campaign admin)
/// * `data` - The new role details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn create_role(
mut transaction: DBTransaction<'_>,
State(mut state): State<AppState>,
Path(id): Path<i64>,
_admin: CampaignAdmin,
Json(data): Json<RoleUpdate>,
) -> Result<impl IntoResponse, ChaosError> {
Role::create(id, data, &mut transaction.tx, &mut state.snowflake_generator).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully created role"))
}
/// Retrieves all roles in a campaign.
///
/// This handler allows any authenticated user to view all roles in a campaign.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `id` - The ID of the campaign
/// * `_user` - The authenticated user
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of roles or error
pub async fn get_roles(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_user: AuthUser,
) -> Result<impl IntoResponse, ChaosError> {
let roles = Role::get_all_in_campaign(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(roles)))
}
/// Creates a new application for a campaign.
///
/// This handler allows authenticated users to apply to open campaigns.
///
/// # Arguments
///
/// * `state` - The application state
/// * `id` - The ID of the campaign
/// * `user` - The authenticated user
/// * `_` - Ensures the campaign is open
/// * `transaction` - Database transaction
/// * `data` - The new application details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn create_application(
State(mut state): State<AppState>,
Path(id): Path<i64>,
user: AuthUser,
_: OpenCampaign,
mut transaction: DBTransaction<'_>,
Json(data): Json<NewApplication>,
) -> Result<impl IntoResponse, ChaosError> {
Application::create(
id,
user.user_id,
data,
&mut state.snowflake_generator,
&mut transaction.tx,
)
.await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully created application"))
}
/// Retrieves all applications for a campaign.
///
/// This handler allows campaign admins to view all applications.
///
/// # Arguments
///
/// * `id` - The ID of the campaign
/// * `_admin` - The authenticated user (must be a campaign admin)
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of applications or error
pub async fn get_applications(
Path(id): Path<i64>,
admin: CampaignAdmin,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let applications = Application::get_from_campaign_id(id, admin.user_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(applications)))
}
/// Creates a new offer for an application.
///
/// This handler allows campaign admins to create offers for applications.
///
/// # Arguments
///
/// * `id` - The ID of the campaign
/// * `state` - The application state
/// * `_admin` - The authenticated user (must be a campaign admin)
/// * `transaction` - Database transaction
/// * `data` - The new offer details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn create_offer(
Path(id): Path<i64>,
State(mut state): State<AppState>,
_admin: CampaignAdmin,
mut transaction: DBTransaction<'_>,
Json(data): Json<Offer>,
) -> Result<impl IntoResponse, ChaosError> {
let _ = Offer::create(
id,
data.application_id,
data.email_template_id,
data.role_id,
data.expiry,
&mut transaction.tx,
&mut state.snowflake_generator,
)
.await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully created offer"))
}
/// Retrieves all offers for a campaign.
///
/// This handler allows campaign admins to view all offers.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `id` - The ID of the campaign
/// * `_user` - The authenticated user (must be a campaign admin)
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of offers or error
pub async fn get_offers(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_user: CampaignAdmin,
) -> Result<impl IntoResponse, ChaosError> {
let offers = Offer::get_by_campaign(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(offers)))
}
}