Skip to content

Commit b1ce539

Browse files
committed
Improve client update logic and validation
Added validation to ensure both updateIfExist and updateKey are set together. Fixed field name from firstName to firstname and corrected update result check from modifiedCount to matchedCount. Updated tests to cover phone change during client update.
1 parent 8bc3b49 commit b1ce539

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

router/admin/client/createClient.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Campaign } from '../../../Models/Campaign';
55
import { Client } from '../../../Models/Client';
66
import { log } from '../../../tools/log';
77
import { 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],

tests/admin/client/createClient.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,26 @@ describe('post on /admin/client/createClient', () => {
158158
expect(res.status).toBe(200);
159159
const client = await Client.findOne({ phone: '+33134567890' });
160160
expect(client?.name).toBe('createClienttestUpdated');
161+
expect(client?.firstname).toBe('createClienttestUpdated');
162+
expect(client?.institution).toBe('createClienttestUpdated');
163+
});
164+
165+
it('should return 200 if updating existing client but change phone', async () => {
166+
const res = await request(app).post('/admin/client/createClient').send({
167+
phone: '+33134567893',
168+
name: 'createClienttestUpdated',
169+
adminCode: adminPassword,
170+
firstName: 'createClienttestUpdated',
171+
institution: 'createClienttestUpdated',
172+
area: areaId,
173+
allreadyHaseded: true,
174+
updateKey: clientId,
175+
updateIfExist: true
176+
});
177+
expect(res.status).toBe(200);
178+
const client = await Client.findOne({ phone: '+33134567890' });
179+
expect(client?.name).toBe('createClienttestUpdated');
180+
expect(client?.firstname).toBe('createClienttestUpdated');
181+
expect(client?.institution).toBe('createClienttestUpdated');
161182
});
162183
});

0 commit comments

Comments
 (0)