11import { Request , Response } from 'express' ;
22
33import { Area } from '../../../Models/Area' ;
4+ import { Campaign } from '../../../Models/Campaign' ;
45import { Client } from '../../../Models/Client' ;
56import { log } from '../../../tools/log' ;
67import { checkParameters , clearPhone , hashPasword , phoneNumberCheck , sanitizeString } from '../../../tools/utils' ;
78
89/**
910 * create a client
11+ * if this client is updated, the updateKey value is reqired
1012 *
1113 * @example
1214 * body:{
1315 * phone: string,
1416 * name: string,
1517 * adminCode: string,
1618 * pinCode: string,
17- * area: string ,
19+ * area: ObjectId ,
1820 * "allreadyHased": boolean
21+ * firstName?: string,
22+ * institution?: string,
23+ * updateIfExist?: boolean,
24+ * updateKey: ObjectId
1925 * }
2026 *
2127 * @throws {400 } if missing parameters
@@ -43,14 +49,37 @@ export default async function createClient(req: Request<any>, res: Response<any>
4349 [ 'institution' , 'string' , true ] ,
4450 [ 'adminCode' , 'string' ] ,
4551 [ 'area' , 'ObjectId' ] ,
46- [ 'allreadyHaseded' , 'boolean' , true ]
52+ [ 'allreadyHaseded' , 'boolean' , true ] ,
53+ [ 'updateIfExist' , 'boolean' , true ] ,
54+ [ 'updateKey' , 'ObjectId' , true ]
4755 ] ,
4856 __filename
4957 )
5058 )
5159 return ;
5260 const password = hashPasword ( req . body . adminCode , req . body . allreadyHaseded , res ) ;
5361 if ( ! password ) return ;
62+
63+ if (
64+ req . body . priority &&
65+ ( ! Array . isArray ( req . body . priority ) ||
66+ req . body . priority . every (
67+ ( e : any ) =>
68+ ! e . campaign ||
69+ ! e . id ||
70+ typeof e . campaign !== 'string' ||
71+ typeof e . id !== 'string' ||
72+ ( e . id . length != 8 && e . id != '-1' ) // Allow '-1' for default priority
73+ ) )
74+ ) {
75+ res . status ( 400 ) . send ( {
76+ message : 'Invalid priority, priority must be a array<{ campaign: objectId, id: string(lenght=8) }>' ,
77+ OK : false
78+ } ) ;
79+ log ( `[!${ req . body . area } , ${ ip } ] Invalid priority` , 'WARNING' , __filename ) ;
80+ return ;
81+ }
82+
5483 const area = await Area . findOne ( { adminPassword : { $eq : password } , _id : { $eq : req . body . area } } ) ;
5584 if ( ! area ) {
5685 res . status ( 401 ) . send ( { message : 'Wrong admin code' , OK : false } ) ;
@@ -65,24 +94,64 @@ export default async function createClient(req: Request<any>, res: Response<any>
6594 return ;
6695 }
6796
68- if ( ( await Client . findOne ( { phone : phone } ) ) != null ) {
69- res . status ( 401 ) . send ( { message : 'User already exist' , OK : false } ) ;
97+ const exist = ( await Client . findOne ( { phone : phone } ) ) != null ;
98+ if ( ! req . body . updateIfExist && exist ) {
99+ res . status ( 422 ) . send ( { message : 'User already exist' , OK : false } ) ;
70100 log ( `[${ req . body . area } , ${ ip } ] User already exist` , 'WARNING' , __filename ) ;
71101 return ;
72102 }
73103
74- const user = new Client ( {
75- name : sanitizeString ( req . body . name ) ,
76- phone : phone ,
77- firstName : sanitizeString ( req . body . firstName ?? '' ) ,
78- institution : sanitizeString ( req . body . institution ?? '' ) ,
79- area : area . _id
80- } ) ;
104+ const campaign = await Campaign . findOne ( { area : { $eq : area . _id } , active : true } , [ ] ) ;
105+ if ( ! campaign ) {
106+ res . status ( 404 ) . send ( { message : 'no campaign in progress' , OK : false } ) ;
107+ log ( `[${ req . body . area } , ${ ip } ] no campaign in progress` , 'WARNING' , __filename ) ;
108+ return ;
109+ }
110+
111+ let client : InstanceType < typeof Client > ;
112+ if ( exist ) {
113+ if ( ( await Client . countDocuments ( { phone, _id : { $not : { $eq : req . body . updateKey } } } ) ) != 0 ) {
114+ res . status ( 422 ) . send ( { message : 'phone number already exist' , OK : false } ) ;
115+ log ( `[${ req . body . area } , ${ ip } ] phone number already exist` , 'WARNING' , __filename ) ;
116+ return ;
117+ }
118+ const client = await Client . updateOne (
119+ { _id : req . body . updateKey } ,
120+ {
121+ name : sanitizeString ( req . body . name ) ,
122+ phone : phone ,
123+ firstName : sanitizeString ( req . body . firstName ?? '' ) ,
124+ institution : sanitizeString ( req . body . institution ?? '' ) ,
125+ area : area . _id ,
126+ campaigns : [ campaign . _id ] ,
127+ priority : req . body . priority ?? [ { campaign : campaign . _id , id : '-1' } ]
128+ }
129+ ) ;
130+ if ( client . modifiedCount === 0 ) {
131+ res . status ( 404 ) . send ( { message : 'Client not found' , OK : false } ) ;
132+ log ( `[${ req . body . area } , ${ ip } ] Client not found` , 'WARNING' , __filename ) ;
133+ return ;
134+ } else {
135+ res . status ( 200 ) . send ( { message : 'Client updated' , OK : true } ) ;
136+ log ( `[${ req . body . area } , ${ ip } ] Client updated` , 'INFO' , __filename ) ;
137+ return ;
138+ }
139+ } else {
140+ client = new Client ( {
141+ name : sanitizeString ( req . body . name ) ,
142+ phone : phone ,
143+ firstName : sanitizeString ( req . body . firstName ?? '' ) ,
144+ institution : sanitizeString ( req . body . institution ?? '' ) ,
145+ area : area . _id ,
146+ campaigns : [ campaign . _id ] ,
147+ priority : req . body . priority ?? [ { campaign : campaign . _id , id : '-1' } ]
148+ } ) ;
149+ }
81150
82151 try {
83- await user . save ( ) ;
84- res . status ( 200 ) . send ( { message : 'user ' + user . name + ' created' , OK : true } ) ;
85- log ( `[${ req . body . area } , ${ ip } ] user ${ user . name } created` , 'INFO' , __filename ) ;
152+ await client . save ( ) ;
153+ res . status ( 200 ) . send ( { message : 'client ' + client . name + ' created' , OK : true } ) ;
154+ log ( `[${ req . body . area } , ${ ip } ] client ${ client . name } created` , 'INFO' , __filename ) ;
86155 } catch ( error : any ) {
87156 res . status ( 500 ) . send ( { message : 'Internal server error' , OK : false } ) ;
88157 log ( `[${ req . body . area } , ${ ip } ] Internal server error: ${ error . message } ` , 'ERROR' , __filename ) ;
0 commit comments