forked from ustaxcourt/ef-cms
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMessage.js
223 lines (212 loc) · 7.86 KB
/
Message.js
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
const joi = require('joi');
const {
joiValidationDecorator,
validEntityDecorator,
} = require('./JoiValidationDecorator');
const { CASE_STATUS_TYPES } = require('./EntityConstants');
const { createISODateString } = require('../utilities/DateHandler');
const { JoiValidationConstants } = require('./JoiValidationConstants');
/**
* constructor
*
* @param {object} rawMessage the raw message data
* @constructor
*/
function Message() {
this.entityName = 'Message';
}
Message.prototype.init = function init(rawMessage, { applicationContext }) {
messageDecorator(this, rawMessage, { applicationContext });
};
const messageDecorator = (
messageEntity,
rawMessage,
{ applicationContext },
) => {
if (!applicationContext) {
throw new TypeError('applicationContext must be defined');
}
messageEntity.attachments = (rawMessage.attachments || []).map(
attachment => ({
documentId: attachment.documentId,
}),
);
messageEntity.caseStatus = rawMessage.caseStatus;
messageEntity.caseTitle = rawMessage.caseTitle;
messageEntity.completedAt = rawMessage.completedAt;
messageEntity.completedBy = rawMessage.completedBy;
messageEntity.completedBySection = rawMessage.completedBySection;
messageEntity.completedByUserId = rawMessage.completedByUserId;
messageEntity.completedMessage = rawMessage.completedMessage;
messageEntity.createdAt = rawMessage.createdAt || createISODateString();
messageEntity.leadDocketNumber = rawMessage.leadDocketNumber;
messageEntity.docketNumber = rawMessage.docketNumber;
messageEntity.docketNumberWithSuffix = rawMessage.docketNumberWithSuffix;
messageEntity.from = rawMessage.from;
messageEntity.fromSection = rawMessage.fromSection;
messageEntity.fromUserId = rawMessage.fromUserId;
messageEntity.isCompleted = rawMessage.isCompleted || false;
messageEntity.isRead = rawMessage.isRead || false;
messageEntity.isRepliedTo = rawMessage.isRepliedTo || false;
messageEntity.message = rawMessage.message;
messageEntity.messageId =
rawMessage.messageId || applicationContext.getUniqueId();
messageEntity.parentMessageId =
rawMessage.parentMessageId || messageEntity.messageId;
messageEntity.subject = rawMessage.subject;
messageEntity.to = rawMessage.to;
messageEntity.toSection = rawMessage.toSection;
messageEntity.toUserId = rawMessage.toUserId;
};
Message.VALIDATION_ERROR_MESSAGES = {
message: [
{ contains: 'is required', message: 'Enter a message' },
{
contains: 'must be less than or equal to',
message: 'Limit is 700 characters. Enter 700 or fewer characters.',
},
],
subject: [
{ contains: 'is required', message: 'Enter a subject line' },
{ contains: 'is not allowed to be empty', message: 'Enter a subject line' },
{
contains: 'must be less than or equal to',
message: 'Limit is 250 characters. Enter 250 or fewer characters.',
},
],
toSection: 'Select a section',
toUserId: 'Select a recipient',
};
Message.VALIDATION_RULES = {
attachments: joi
.array()
.items(
joi.object().keys({
documentId: JoiValidationConstants.UUID.required().description(
'ID of the document attached; can be either a docketEntryId or correspondenceId depending on the type of document.',
),
documentTitle: JoiValidationConstants.STRING.max(500).optional(),
}),
)
.optional()
.description('Array of document metadata objects attached to the message.'),
caseStatus: JoiValidationConstants.STRING.valid(
...Object.values(CASE_STATUS_TYPES),
)
.required()
.description('The status of the associated case.'),
caseTitle: JoiValidationConstants.STRING.required().description(
'The case title for the associated cases.',
),
completedAt: JoiValidationConstants.ISO_DATE.when('isCompleted', {
is: true,
otherwise: joi.optional().allow(null),
then: joi.required(),
}).description('When the message was marked as completed.'),
completedBy: JoiValidationConstants.STRING.max(500)
.when('isCompleted', {
is: true,
otherwise: joi.optional().allow(null),
then: joi.required(),
})
.description('The name of the user who completed the message thread'),
completedBySection: JoiValidationConstants.STRING.when('isCompleted', {
is: true,
otherwise: joi.optional().allow(null),
then: joi.required(),
}).description('The section of the user who completed the message thread'),
completedByUserId: JoiValidationConstants.UUID.when('isCompleted', {
is: true,
otherwise: joi.optional().allow(null),
then: joi.required(),
}).description('The ID of the user who completed the message thread'),
completedMessage: JoiValidationConstants.STRING.max(500)
.allow(null)
.optional()
.description('The message entered when completing the message thread.'),
createdAt: JoiValidationConstants.ISO_DATE.required().description(
'When the message was created.',
),
docketNumber: JoiValidationConstants.DOCKET_NUMBER.required(),
docketNumberWithSuffix: JoiValidationConstants.STRING.max(20)
.required()
.description('The docket number and suffix for the associated case.'),
entityName: JoiValidationConstants.STRING.valid('Message').required(),
from: JoiValidationConstants.STRING.max(100)
.required()
.description('The name of the user who sent the message.'),
fromSection: JoiValidationConstants.STRING.required().description(
'The section of the user who sent the message.',
),
fromUserId: JoiValidationConstants.UUID.required().description(
'The ID of the user who sent the message.',
),
isCompleted: joi
.boolean()
.required()
.description('Whether the message thread has been completed.'),
isRead: joi.boolean().required(),
isRepliedTo: joi
.boolean()
.required()
.description('Whether the message has been replied to or forwarded.'),
leadDocketNumber: JoiValidationConstants.DOCKET_NUMBER.optional(),
message: JoiValidationConstants.STRING.max(700)
.required()
.description('The message text.'),
messageId: JoiValidationConstants.UUID.required().description(
'A unique ID generated by the system to represent the message.',
),
parentMessageId: JoiValidationConstants.UUID.required().description(
'The ID of the initial message in the thread.',
),
subject: JoiValidationConstants.STRING.max(250)
.required()
.description('The subject line of the message.'),
to: JoiValidationConstants.STRING.max(100)
.required()
.allow(null)
.description('The name of the user who is the recipient of the message.'),
toSection: JoiValidationConstants.STRING.required().description(
'The section of the user who is the recipient of the message.',
),
toUserId: JoiValidationConstants.UUID.required()
.allow(null)
.description('The ID of the user who is the recipient of the message.'),
};
joiValidationDecorator(
Message,
joi.object().keys(Message.VALIDATION_RULES),
Message.VALIDATION_ERROR_MESSAGES,
);
/**
* marks the message as completed by the user at the current time
*
* @param {string} message the message provided when completing the thread
* @param {object} user the user who completed the thread
* @returns {Message} the updated message
*/
Message.prototype.markAsCompleted = function ({ message, user }) {
this.isCompleted = true;
this.completedAt = createISODateString();
this.isRepliedTo = true;
this.completedBy = user.name;
this.completedByUserId = user.userId;
this.completedBySection = user.section;
this.completedMessage = message || null;
return this;
};
/**
* adds the attachment to the attachments array on the message
*
* @param {object} attachmentToAdd the attachment to add to the message
* @returns {Message} the updated message
*/
Message.prototype.addAttachment = function (attachmentToAdd) {
this.attachments.push(attachmentToAdd);
return this;
};
module.exports = {
Message: validEntityDecorator(Message),
messageDecorator,
};