-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-offer-request.js
205 lines (187 loc) · 5.79 KB
/
get-offer-request.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
// Import necessary modules and models
const { roles } = require('../../role/constants');
const {
OfferRequest,
Customer,
OfferRequestProgress,
User,
Sequelize,
sequelize,
} = require('../../models');
// Define an asynchronous function to handle the request
const getOfferRequestsV1 = async (req, res, next) => {
/*
This function retrieves a list of offer requests based on various query parameters.
It also handles pagination and filtering based on user roles and request parameters.
*/
try {
// Extract query parameters from the request
const {
status,
progress,
exclude = {},
isAssigned,
q,
sortBy,
sortOrder = 'ASC',
page = {},
} = req.query;
// Set default limit and offset values for pagination
let limit = 10;
let offset = 0;
if (page.limit) {
limit = Number(page.limit);
}
if (page.offset) {
offset = Number(page.offset);
}
// Get the user information from the request
const { user } = req;
// Define the base query object
const query = {
limit,
offset,
distinct: true,
where: {
id: {
[Sequelize.Op.and]: [],
},
},
order: [],
include: [
{
model: User,
as: 'users',
through: { attributes: [] },
attributes: [],
},
{
model: Customer,
as: 'customer',
attributes: [],
},
{
model: OfferRequestProgress,
as: 'offerRequestProgress',
attributes: [],
},
],
};
// Include OfferRequestProgress model if 'progress' parameter is provided
if (progress) {
query.include[2].required = true;
query.include[2].where = { event: progress };
}
// Exclude offer requests with specific status if 'exclude.status' parameter is provided
if (exclude.status) {
const excludeQuery = sequelize.dialect.queryGenerator
.selectQuery('OfferRequestProgresses', {
attributes: ['offerRequestId'],
where: {
event: `${exclude.status}`,
},
})
.slice(0, -1);
query.where.id[Sequelize.Op.and].push({
[Sequelize.Op.notIn]: Sequelize.literal(`(${excludeQuery})`),
});
}
// Include offer requests with specific status if 'status' parameter is provided
if (status) {
query.where.id[Sequelize.Op.and].push({
[Sequelize.Op.in]: Sequelize.literal(
`(SELECT offerRequestId FROM OfferRequestProgresses WHERE createdAt = (SELECT MAX(createdAt) FROM OfferRequestProgresses AS subquery WHERE subquery.offerRequestId = OfferRequestProgresses.offerRequestId) AND event = '${status}')`
),
});
}
// Include only assigned offer requests if 'isAssigned' is 'true'
if (isAssigned === 'true') {
query.include[0].required = true;
}
// Exclude assigned offer requests if 'isAssigned' is 'false'
if (isAssigned === 'false') {
const isAssignedQuery = sequelize.dialect.queryGenerator
.selectQuery('Assignments', {
attributes: ['offerRequestId'],
where: {
deletedAt: null,
},
})
.slice(0, -1);
query.where.id[Sequelize.Op.and].push({
[Sequelize.Op.notIn]: Sequelize.literal(`(${isAssignedQuery})`),
});
}
// Sort the results based on the 'sortBy' and 'sortOrder' parameters
if (sortBy === 'assignedTo') {
query.order.push(['users', 'firstName', sortOrder]);
} else if (sortBy === 'submittedBy') {
query.order.push(['customer', 'firstName', sortOrder]);
} else if (sortBy) {
query.order.push([sortBy, sortOrder]);
}
// Perform a search query if 'q' parameter is provided
if (q) {
query.include[1].required = true;
query.include[1].where = {
[Sequelize.Op.or]: [
{ firstName: { [Sequelize.Op.like]: `%${q}%` } },
{ lastName: { [Sequelize.Op.like]: `%${q}%` } },
{ address: { [Sequelize.Op.like]: `%${q}%` } },
],
};
}
// Restrict access for non-admin users
if (!user.roles.includes(roles.ADMIN)) {
query.include[0].required = true;
query.include[0].where = { id: user.id };
}
// Execute the query to find and count offer requests
const response = await OfferRequest.findAndCountAll(query);
const { rows, count } = response;
// If no results are found, return a 204 No Content response
if (rows.length === 0) {
res.status(204).json();
return;
}
// Build pagination URLs
let queryParams = '';
Object.keys(req.query).forEach((key) => {
if (key !== 'page') {
queryParams += `${key}=${req.query[key]}&`;
}
});
const fullUrl = `${req.originalUrl.split('?')[0]}?${queryParams}`;
const firstPage = `${fullUrl}page[offset]=${0}&page[limit]=${limit}`;
const lastPage = `${fullUrl}page[offset]=${
count > limit ? count - limit : 0
}&page[limit]=${limit}`;
let prevPage = null;
let nextPage = null;
if (offset > 0) {
prevPage = `${fullUrl}page[offset]=${
offset > limit ? offset - limit : 0
}&page[limit]=${offset > limit ? limit : offset}`;
}
if (count > offset + limit) {
nextPage = `${fullUrl}page[offset]=${
offset + limit
}&page[limit]=${limit}`;
}
// Return the results and pagination information in the response
res.status(200).json({
data: { offerRequests: rows },
pagination: {
first: firstPage,
last: lastPage,
prev: prevPage,
next: nextPage,
},
});
} catch (err) {
// Handle any errors by passing them to the error handling middleware
next(err);
}
};
// Export the function for use in other modules
module.exports = { getOfferRequestsV1 };