-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
84 lines (82 loc) · 3.2 KB
/
create.js
File metadata and controls
84 lines (82 loc) · 3.2 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
/**
* @fileoverview Celebration Creation Controller
*
* This controller handles the creation of celebration records in the database.
* It creates the celebration document with all required fields including
* donation amount, politician information, donor information snapshot, and
* payment intent details. The celebration is created in 'active' status
* awaiting trigger condition.
*
* BUSINESS LOGIC
*
* CELEBRATION CREATION
* - Creates celebration document with req.body data
* - Sets initial status to 'active' with status_ledger entry
* - Stores immutable donor information snapshot for FEC compliance
* - Links payment_intent (created but not charged)
* - Records idempotencyKey to prevent duplicates
*
* STATUS INITIALIZATION
* - Initial status entry created via StatusService
* - Records creation metadata in status_ledger
* - Sets legacy boolean flags (resolved: false, defunct: false, paused: false)
*
* DEPENDENCIES
* - models/Celebration: Celebration model for database operations
* - services/utils/logger: Logging
*
* @module controller/celebrations/create
* @requires ../../services/utils/logger
* @requires ../../models/Celebration
*/
const logger = require('../../services/utils/logger')(__filename);
module.exports = {
/**
* Creates a new celebration record in the database
*
* This function creates a celebration document with all required fields
* including donation amount, politician information, donor information
* snapshot, and payment intent details. The celebration is created in
* 'active' status awaiting trigger condition.
*
* @param {Object} req - Express request object
* @param {Object} req.body - Celebration creation data
* @param {string} req.body.donatedBy - User ID making the donation
* @param {number} req.body.donation - Donation amount in dollars
* @param {number} req.body.fee - Stripe processing fee
* @param {string} req.body.pol_id - Politician ID receiving the donation
* @param {string} req.body.bill_id - Bill ID associated with the donation
* @param {Object} req.body.donorInfo - Donor information snapshot
* @param {string} req.body.idempotencyKey - Unique key for idempotency
* @param {Object} res - Express response object (not used, returns document)
* @param {Object} model - Celebration model for database operations
* @returns {Promise<Object>} Created celebration document
* @throws {Error} Database error if creation fails
*
* @example
* ```javascript
* const { create } = require('./controller/celebrations/create');
* const celebration = await create(req, res, Celebration);
* ```
*/
create: async (req, res, model) => {
try {
const doc = await model.create(req.body);
logger.info('Celebration created', {
action: 'create_celebration',
userId: req.jwt?.payload?.sub,
id: doc._id,
ip: req.ip,
});
return doc; // Return the document instead of sending response
} catch (err) {
logger.error('Failed to create celebration', {
action: 'create_celebration',
userId: req.jwt?.payload?.sub,
error: err.message,
ip: req.ip,
});
throw err; // Throw error instead of sending response
}
},
};