tango-app-api-client 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/controllers/client.controllers.js +172 -2
- package/src/dtos/client.dtos.js +21 -0
- package/src/routes/client.routes.js +6 -3
- package/src/service/camera.service.js +9 -0
- package/src/service/client.service.js +9 -1
- package/src/service/store.service.js +9 -0
- package/src/service/user.service.js +9 -0
- package/src/validations/client.validations.js +22 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tango-app-api-client",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "client",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -17,9 +17,10 @@
|
|
|
17
17
|
"dotenv": "^16.4.4",
|
|
18
18
|
"express": "^4.18.2",
|
|
19
19
|
"handlebars": "^4.7.8",
|
|
20
|
+
"lodash": "^4.17.21",
|
|
20
21
|
"mongodb": "^6.3.0",
|
|
21
22
|
"nodemon": "^3.0.3",
|
|
22
|
-
"tango-api-schema": "^2.0.
|
|
23
|
+
"tango-api-schema": "^2.0.33",
|
|
23
24
|
"tango-app-api-middleware": "^1.0.16",
|
|
24
25
|
"winston": "^3.11.0",
|
|
25
26
|
"winston-daily-rotate-file": "^5.0.0"
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { logger } from 'tango-app-api-middleware';
|
|
2
|
-
import { createAuditQueue, findOne, getClientCount, insert, update,
|
|
2
|
+
import { createAuditQueue, findOne, getClientCount, insert, update, findClient, aggregateClient } from '../service/client.service.js';
|
|
3
|
+
import { countDocumentsUser, findOneUser } from '../service/user.service.js';
|
|
4
|
+
import { aggregateStore, countDocumentsStore } from '../service/store.service.js';
|
|
5
|
+
import { aggregateCamera, countDocumentsCamera } from '../service/camera.service.js';
|
|
6
|
+
import _ from 'lodash';
|
|
3
7
|
|
|
4
8
|
export async function create( req, res ) {
|
|
5
9
|
try {
|
|
@@ -62,7 +66,7 @@ export async function changeStatus( req, res, next ) {
|
|
|
62
66
|
export async function getClients( req, res ) {
|
|
63
67
|
try {
|
|
64
68
|
const field = { clientName: 1, clientId: 1 };
|
|
65
|
-
const result = await
|
|
69
|
+
const result = await findClient( {}, field );
|
|
66
70
|
if ( result == 0 ) {
|
|
67
71
|
return res.sendError( 'No Data Found', 204 );
|
|
68
72
|
}
|
|
@@ -72,3 +76,169 @@ export async function getClients( req, res ) {
|
|
|
72
76
|
return res.sendError( error, 500 );
|
|
73
77
|
}
|
|
74
78
|
}
|
|
79
|
+
|
|
80
|
+
export async function detailedAllClientCount( req, res ) {
|
|
81
|
+
try {
|
|
82
|
+
const query = [
|
|
83
|
+
{
|
|
84
|
+
$project: {
|
|
85
|
+
activeClient: { $cond: [ { $eq: [ '$status', 'active' ] }, 1, 0 ] },
|
|
86
|
+
paidClient: { $cond: [ { $eq: [ '$status', 'paid' ] }, 1, 0 ] },
|
|
87
|
+
trialClient: { $cond: [ { $eq: [ '$status', 'trial' ] }, 1, 0 ] },
|
|
88
|
+
freeClient: { $cond: [ { $eq: [ '$status', 'free' ] }, 1, 0 ] },
|
|
89
|
+
holdClient: { $cond: [ { $eq: [ '$status', 'hold' ] }, 1, 0 ] },
|
|
90
|
+
suspendClient: { $cond: [ { $eq: [ '$status', 'suspend' ] }, 1, 0 ] },
|
|
91
|
+
deactiveClient: { $cond: [ { $eq: [ '$status', 'deactive' ] }, 1, 0 ] },
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
$group: {
|
|
96
|
+
_id: null,
|
|
97
|
+
totalCount: { $sum: 1 },
|
|
98
|
+
activeClient: { $sum: '$activeClient' },
|
|
99
|
+
paidClient: { $sum: '$paidClient' },
|
|
100
|
+
trialClient: { $sum: '$trialClient' },
|
|
101
|
+
freeClient: { $sum: '$freeClient' },
|
|
102
|
+
holdClient: { $sum: '$holdClient' },
|
|
103
|
+
suspendClient: { $sum: '$suspendClient' },
|
|
104
|
+
deactiveClient: { $sum: '$deactiveClient' },
|
|
105
|
+
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
];
|
|
109
|
+
const result = await aggregateClient( query );
|
|
110
|
+
if ( result == 0 ) {
|
|
111
|
+
return res.sendError( 'No Data Found', 204 );
|
|
112
|
+
}
|
|
113
|
+
return res.sendSuccess( { result: result } );
|
|
114
|
+
} catch ( error ) {
|
|
115
|
+
logger.error( { error: error, function: 'detailedAllClientCount' } );
|
|
116
|
+
return res.sendError( error, 500 );
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export async function clientList( req, res ) {
|
|
121
|
+
try {
|
|
122
|
+
const inputData = req.body;
|
|
123
|
+
const limit =inputData.limit || 10;
|
|
124
|
+
const skip = inputData.offset? ( inputData.offset - 1 ) * limit : 0;
|
|
125
|
+
let clientQuery = [
|
|
126
|
+
{
|
|
127
|
+
$project: {
|
|
128
|
+
status: 1,
|
|
129
|
+
clientName: 1,
|
|
130
|
+
clientId: 1,
|
|
131
|
+
subscriptionType: 1,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
const clientCount = await aggregateClient( clientQuery );
|
|
136
|
+
if ( clientCount.length == 0 ) {
|
|
137
|
+
return res.sendError( { error: 'No Data Found' }, 204 );
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if ( inputData.searchValue ) {
|
|
141
|
+
clientQuery.push( {
|
|
142
|
+
$match: {
|
|
143
|
+
$or: [
|
|
144
|
+
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
145
|
+
{ clientName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
146
|
+
{ subscriptionType: { $regex: inputData.searchValue, $options: 'i' } },
|
|
147
|
+
{ status: { $regex: inputData.searchValue, $options: 'i' } },
|
|
148
|
+
],
|
|
149
|
+
|
|
150
|
+
},
|
|
151
|
+
} );
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if ( inputData.sortColumName ) {
|
|
155
|
+
clientQuery.push( {
|
|
156
|
+
$sort: { [inputData.sortColumName]: inputData.sortBy || -1 },
|
|
157
|
+
} );
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
clientQuery.push(
|
|
161
|
+
{ $skip: skip },
|
|
162
|
+
{ $limit: limit },
|
|
163
|
+
);
|
|
164
|
+
const client = await aggregateClient( clientQuery );
|
|
165
|
+
const clientList = client.map( ( item ) => item.clientId );
|
|
166
|
+
const storeQuery =[
|
|
167
|
+
{
|
|
168
|
+
$match: {
|
|
169
|
+
$and: [
|
|
170
|
+
{ status: { $eq: 'active' } },
|
|
171
|
+
{ clientId: { $in: clientList } },
|
|
172
|
+
],
|
|
173
|
+
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
$group: {
|
|
178
|
+
_id: '$clientId',
|
|
179
|
+
clientId: { $first: '$clientId' },
|
|
180
|
+
activeStoreCount: { $sum: 1 },
|
|
181
|
+
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
];
|
|
185
|
+
|
|
186
|
+
const cameraQuery =[
|
|
187
|
+
{
|
|
188
|
+
$match: {
|
|
189
|
+
$and: [
|
|
190
|
+
{ isActivated: { $eq: true } },
|
|
191
|
+
{ isUp: { $eq: true } },
|
|
192
|
+
{ clientId: { $in: clientList } },
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
$group: {
|
|
198
|
+
_id: '$clientId',
|
|
199
|
+
clientId: { $first: '$clientId' },
|
|
200
|
+
activeCameraCount: { $sum: 1 },
|
|
201
|
+
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
const activeStoreCount = await aggregateStore( storeQuery );
|
|
207
|
+
const activeCameraCount = await aggregateCamera( cameraQuery );
|
|
208
|
+
const result =_.values(
|
|
209
|
+
_.merge(
|
|
210
|
+
_.keyBy( client, 'clientId' ),
|
|
211
|
+
_.keyBy( activeStoreCount, 'clientId' ),
|
|
212
|
+
_.keyBy( activeCameraCount, 'clientId' ),
|
|
213
|
+
),
|
|
214
|
+
);
|
|
215
|
+
return res.sendSuccess( { result: result, count: clientCount.length } );
|
|
216
|
+
} catch ( error ) {
|
|
217
|
+
logger.error( { error: error, function: 'clientList' } );
|
|
218
|
+
return res.sendError( error, 500 );
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export async function detailedClientCount( req, res ) {
|
|
223
|
+
try {
|
|
224
|
+
const inputData = req.query;
|
|
225
|
+
let result = {
|
|
226
|
+
clientName: inputData.clientName,
|
|
227
|
+
role: 'admin',
|
|
228
|
+
};
|
|
229
|
+
result['totalStoreCount'] = await countDocumentsStore( { clientId: inputData.clientId } );
|
|
230
|
+
result['totalGroupCount'] = await countDocumentsStore( { clientId: inputData.clientId } );
|
|
231
|
+
result['totalCameraCount'] = await countDocumentsCamera( { clientId: inputData.clientId } );
|
|
232
|
+
result['totalUserCount'] = await countDocumentsUser( { clientId: inputData.clientId, userType: 'client' } );
|
|
233
|
+
|
|
234
|
+
const user = await findOneUser( { _id: inputData.userId }, { userName: 1, email: 1 } );
|
|
235
|
+
result['userName'] = user?.userName;
|
|
236
|
+
result['email'] = user?.email;
|
|
237
|
+
result['profileCompletion'] = 70;
|
|
238
|
+
|
|
239
|
+
return res.sendSuccess( { result: result } );
|
|
240
|
+
} catch ( error ) {
|
|
241
|
+
logger.error( { error: error, function: 'detailedClientCount' } );
|
|
242
|
+
return res.sendError( error, 500 );
|
|
243
|
+
}
|
|
244
|
+
}
|
package/src/dtos/client.dtos.js
CHANGED
|
@@ -8,3 +8,24 @@ export const clientCreationSchema = joi.object( {
|
|
|
8
8
|
export const clientCreationValid = {
|
|
9
9
|
body: clientCreationSchema,
|
|
10
10
|
};
|
|
11
|
+
|
|
12
|
+
export const detailedClientCountSchema = joi.object( {
|
|
13
|
+
clientId: joi.string().required(),
|
|
14
|
+
} );
|
|
15
|
+
|
|
16
|
+
export const detailedClientCountValid = {
|
|
17
|
+
query: detailedClientCountSchema,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const clientListSchema = joi.object( {
|
|
21
|
+
isExport: joi.boolean().optional(),
|
|
22
|
+
searchValue: joi.string().optional(),
|
|
23
|
+
sortColumName: joi.string().optional(),
|
|
24
|
+
sortBy: joi.number().optional(),
|
|
25
|
+
limit: joi.number().optional(),
|
|
26
|
+
offset: joi.number().optional(),
|
|
27
|
+
} );
|
|
28
|
+
|
|
29
|
+
export const clientListValid = {
|
|
30
|
+
body: clientListSchema,
|
|
31
|
+
};
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
|
|
2
2
|
import express from 'express';
|
|
3
3
|
import { validate } from 'tango-app-api-middleware';
|
|
4
|
-
import { clientCreationValid } from '../dtos/client.dtos.js';
|
|
5
|
-
import { isclientNameExists } from '../validations/client.validations.js';
|
|
6
|
-
import { changeStatus, create, getClients } from '../controllers/client.controllers.js';
|
|
4
|
+
import { clientCreationValid, clientListValid, detailedClientCountValid } from '../dtos/client.dtos.js';
|
|
5
|
+
import { isclientIdExists, isclientNameExists } from '../validations/client.validations.js';
|
|
6
|
+
import { changeStatus, create, getClients, detailedAllClientCount, detailedClientCount, clientList } from '../controllers/client.controllers.js';
|
|
7
7
|
|
|
8
8
|
export const clientRouter = express.Router();
|
|
9
9
|
|
|
10
10
|
clientRouter.post( '/create', validate( clientCreationValid ), isclientNameExists, changeStatus, create );
|
|
11
11
|
clientRouter.get( '/get-clients', getClients );
|
|
12
|
+
clientRouter.get( '/detailed-all-client-count', detailedAllClientCount );
|
|
13
|
+
clientRouter.post( '/client-list', validate( clientListValid ), clientList );
|
|
14
|
+
clientRouter.get( '/detailed-client-count', validate( detailedClientCountValid ), isclientIdExists, detailedClientCount );
|
|
12
15
|
|
|
13
16
|
|
|
@@ -17,7 +17,7 @@ export function insert( record ) {
|
|
|
17
17
|
return model.clientModel.insertMany( record );
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export function
|
|
20
|
+
export function findClient( query, field ) {
|
|
21
21
|
return model.clientModel.find( query, field );
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -44,3 +44,11 @@ export async function createAuditQueue( queueName ) {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
export function aggregateClient( query ) {
|
|
48
|
+
return model.clientModel.aggregate( query );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function findOneClient( query, field ) {
|
|
52
|
+
return model.clientModel.findOne( query, field );
|
|
53
|
+
}
|
|
54
|
+
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import model from 'tango-api-schema';
|
|
2
|
+
import { findOneClient } from '../service/client.service.js';
|
|
2
3
|
|
|
3
4
|
export async function isclientNameExists( req, res, next ) {
|
|
4
5
|
try {
|
|
5
6
|
const inputData = req.body;
|
|
6
|
-
const
|
|
7
|
-
if (
|
|
7
|
+
const client = await model.clientModel.findOne( { clientName: inputData?.clientName } );
|
|
8
|
+
if ( client ) {
|
|
8
9
|
return res.sendError( 'clientName is exists', 403 );
|
|
9
10
|
}
|
|
10
11
|
return next();
|
|
@@ -13,3 +14,22 @@ export async function isclientNameExists( req, res, next ) {
|
|
|
13
14
|
return res.sendError( error, 500 );
|
|
14
15
|
}
|
|
15
16
|
}
|
|
17
|
+
|
|
18
|
+
export async function isclientIdExists( req, res, next ) {
|
|
19
|
+
try {
|
|
20
|
+
const inputData = req.query;
|
|
21
|
+
const query = {
|
|
22
|
+
clientId: inputData?.clientId,
|
|
23
|
+
};
|
|
24
|
+
const client = await findOneClient( query );
|
|
25
|
+
if ( !client ) {
|
|
26
|
+
return res.sendError( { error: `client doesn't exists` }, 204 );
|
|
27
|
+
}
|
|
28
|
+
req.query.clientName = client.clientName;
|
|
29
|
+
req.query.userId = client.userId;
|
|
30
|
+
return next();
|
|
31
|
+
} catch ( error ) {
|
|
32
|
+
logger.error( { error: error, function: 'isclientIdExists' } );
|
|
33
|
+
return res.sendError( error, 500 );
|
|
34
|
+
}
|
|
35
|
+
}
|