@@ -5,6 +5,7 @@ import { Campaign } from '../../../Models/Campaign';
55import { Client } from '../../../Models/Client' ;
66import { log } from '../../../tools/log' ;
77import { checkParameters , clearPhone , hashPasword , phoneNumberCheck , sanitizeString } from '../../../tools/utils' ;
8+ import { ObjectId } from 'mongodb' ;
89
910/**
1011 * create a client
@@ -80,6 +81,16 @@ export default async function createClient(req: Request<any>, res: Response<any>
8081 return ;
8182 }
8283
84+ if ( ( req . body . updateIfExist && ! req . body . updateKey ) || ( req . body . updateKey && ! req . body . updateIfExist ) ) {
85+ res . status ( 400 ) . send ( { message : 'updateIfExist and updateKey must be both set or not set' , OK : false } ) ;
86+ log (
87+ `[!${ req . body . area } , ${ ip } ] updateIfExist and updateKey must be both set or not set` ,
88+ 'WARNING' ,
89+ __filename
90+ ) ;
91+ return ;
92+ }
93+
8394 const area = await Area . findOne ( { adminPassword : { $eq : password } , _id : { $eq : req . body . area } } ) ;
8495 if ( ! area ) {
8596 res . status ( 401 ) . send ( { message : 'Wrong admin code' , OK : false } ) ;
@@ -115,19 +126,20 @@ export default async function createClient(req: Request<any>, res: Response<any>
115126 log ( `[${ req . body . area } , ${ ip } ] phone number already exist` , 'WARNING' , __filename ) ;
116127 return ;
117128 }
129+
118130 const client = await Client . updateOne (
119131 { _id : req . body . updateKey } ,
120132 {
121133 name : sanitizeString ( req . body . name ) ,
122134 phone : phone ,
123- firstName : sanitizeString ( req . body . firstName ?? '' ) ,
135+ firstname : sanitizeString ( req . body . firstName ?? '' ) ,
124136 institution : sanitizeString ( req . body . institution ?? '' ) ,
125137 area : area . _id ,
126138 campaigns : [ campaign . _id ] ,
127139 priority : req . body . priority ?? [ { campaign : campaign . _id , id : '-1' } ]
128140 }
129141 ) ;
130- if ( client . modifiedCount === 0 ) {
142+ if ( client . matchedCount === 0 ) {
131143 res . status ( 404 ) . send ( { message : 'Client not found' , OK : false } ) ;
132144 log ( `[${ req . body . area } , ${ ip } ] Client not found` , 'WARNING' , __filename ) ;
133145 return ;
@@ -140,7 +152,7 @@ export default async function createClient(req: Request<any>, res: Response<any>
140152 client = new Client ( {
141153 name : sanitizeString ( req . body . name ) ,
142154 phone : phone ,
143- firstName : sanitizeString ( req . body . firstName ?? '' ) ,
155+ firstname : sanitizeString ( req . body . firstName ?? '' ) ,
144156 institution : sanitizeString ( req . body . institution ?? '' ) ,
145157 area : area . _id ,
146158 campaigns : [ campaign . _id ] ,
0 commit comments