Skip to content

Commit 67c1784

Browse files
authored
Merge pull request #20 from ThePiratePhone/remove-area-for-caller
Remove area for caller
2 parents d30a483 + 051728d commit 67c1784

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+729
-699
lines changed

Models/Caller.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ const CallerSchema = new mongoose.Schema({
3737
createdAt: {
3838
type: Date,
3939
default: new Date()
40-
},
41-
area: {
42-
type: mongoose.Schema.ObjectId,
43-
ref: 'Area',
44-
required: true
4540
}
4641
});
4742

router/admin/caller/addCallerCampaign.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export default async function addCallerCampaign(req: Request<any>, res: Response
5050

5151
const password = hashPasword(req.body.adminCode, req.body.allreadyHaseded, res);
5252
if (!password) return;
53-
const area = await Area.findOne({ adminPassword: { $eq: password }, _id: { $eq: req.body.area } }, ['name']);
53+
const area = await Area.findOne({ adminPassword: { $eq: password }, _id: { $eq: req.body.area } }, [
54+
'name',
55+
'campaignList'
56+
]);
5457
if (!area) {
5558
res.status(401).send({ message: 'Wrong admin code', OK: false });
5659
log(`[!${req.body.area}, ${ip}] Wrong admin code`, 'WARNING', __filename);
@@ -59,7 +62,9 @@ export default async function addCallerCampaign(req: Request<any>, res: Response
5962

6063
req.body.phone = clearPhone(req.body.phone);
6164

62-
const caller = await Caller.findOne({ phone: { $eq: req.body.phone }, area: area._id }, ['campaigns']);
65+
const caller = await Caller.findOne({ phone: { $eq: req.body.phone }, campaigns: { $in: area.campaignList } }, [
66+
'campaigns'
67+
]);
6368
if (!caller) {
6469
res.status(404).send({ message: 'Caller not found', OK: false });
6570
log(`[${ip}, ${req.body.area}] Caller not found`, 'WARNING', __filename);

router/admin/caller/callerInfo.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ export default async function callerInfo(req: Request<any>, res: Response<any>)
5858

5959
const password = hashPasword(req.body.adminCode, req.body.allreadyHaseded, res);
6060
if (!password) return;
61-
const area = await Area.findOne({ _id: { $eq: req.body.area }, adminPassword: { $eq: password } }, ['name']);
61+
62+
const area = await Area.findOne({ _id: { $eq: req.body.area }, adminPassword: { $eq: password } }, [
63+
'name',
64+
'campaignList'
65+
]);
6266
if (!area) {
6367
res.status(404).send({ message: 'no area found', OK: false });
6468
log(`[!${req.body.area}, ${ip}] no area found`, 'WARNING', __filename);
6569
return;
6670
}
6771

68-
const caller = await Caller.findOne({ phone: { $eq: phone }, area: { $eq: req.body.area } }, [
72+
const caller = await Caller.findOne({ phone: { $eq: phone }, campaigns: { $in: area.campaignList } }, [
6973
'_id',
7074
'name',
7175
'phone'

router/admin/caller/changeCallerPassword.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ export default async function changeCallerPassword(req: Request<any>, res: Respo
6666
log(`[!${req.body.area}, ${ip}] Invalid phone number`, 'WARNING', __filename);
6767
return;
6868
}
69-
const result = await Caller.updateOne({ phone: phone, area: area._id }, { pinCode: req.body.newPassword });
69+
const result = await Caller.updateOne(
70+
{ phone: phone, campaigns: { $in: area.campaignList } },
71+
{ pinCode: req.body.newPassword }
72+
);
7073
if (result.matchedCount != 1) {
7174
res.status(404).send({ message: 'Caller not found or same password', OK: false });
7275
log(`[${ip}, ${req.body.area}] Caller not found or same password from admin`, 'WARNING', __filename);

router/admin/caller/changeName.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export default async function ChangeName(req: Request<any>, res: Response<any>)
5252
log(`[!${req.body.area}, ${ip}] Wrong phone number`, `WARNING`, __filename);
5353
return;
5454
}
55-
req.body.newName = req.body.newName.trim();
56-
if (req.body.newName == ``) {
55+
req.body.newName = sanitizeString(req.body.newName);
56+
if (req.body.newName == `` || req.body.newName.length > 50 || req.body.newName.length < 3) {
5757
res.status(400).send({ message: `Wrong newName`, OK: false });
5858
log(`[!${req.body.area}, ${ip}] Wrong newName`, `WARNING`, __filename);
5959
return;
@@ -68,9 +68,10 @@ export default async function ChangeName(req: Request<any>, res: Response<any>)
6868
return;
6969
}
7070

71-
req.body.newName = sanitizeString(req.body.newName);
72-
73-
const change = await Caller.updateOne({ phone: phone, area: { $eq: req.body.area } }, { name: req.body.newName });
71+
const change = await Caller.updateOne(
72+
{ phone: phone, campaigns: { $in: area.campaignList } },
73+
{ name: req.body.newName }
74+
);
7475
if (change.matchedCount != 1) {
7576
res.status(400).send({ message: `Caller not found`, OK: false });
7677
log(`[${ip}, ${req.body.area}] Caller not found`, `WARNING`, __filename);

router/admin/caller/exportCallerCsv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default async function exportCallerCsv(req: Request<any>, res: Response<a
5151
log(`[!${req.body.area}, ${ip}] Wrong admin code`, 'WARNING', __filename);
5252
return;
5353
}
54-
let selector: {} = { area: area._id };
54+
let selector: {} = {};
5555

5656
const campaign = await Campaign.findOne({ area: area._id, active: true });
5757
if (req.body.curentCamaign) {
@@ -60,7 +60,7 @@ export default async function exportCallerCsv(req: Request<any>, res: Response<a
6060
log(`[${ip}, ${req.body.area}] No campaign in progress`, 'WARNING', __filename);
6161
return;
6262
}
63-
selector = { $or: [{ campaigns: campaign._id }, { area: area._id }] };
63+
selector = { campaigns: campaign._id };
6464
}
6565

6666
const csvStream = csv.format({ headers: true, delimiter: ';' });

router/admin/caller/listCaller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ export default async function listCaller(req: Request<any>, res: Response<any>)
5252
return;
5353
}
5454

55-
const numberOfCallers = await Caller.countDocuments({ area: area.id });
55+
const numberOfCallers = await Caller.countDocuments({ campaigns: { $in: area.campaignList } });
5656

5757
if (numberOfCallers === 0) {
5858
res.status(404).send({ message: 'No caller found', OK: false });
5959
log(`[${ip}, ${req.body.area}] No caller found`, 'WARNING', __filename);
6060
return;
6161
}
62-
const callers = await Caller.find({ area: area._id })
62+
const callers = await Caller.find({ campaigns: { $in: area.campaignList } })
6363
.skip(req.body.skip ? req.body.skip : 0)
6464
.limit(req.body.limit ? req.body.limit : 50);
6565
if (!callers) {

router/admin/caller/newCaller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Area } from '../../../Models/Area';
44
import { Caller } from '../../../Models/Caller';
55
import { log } from '../../../tools/log';
66
import { checkParameters, clearPhone, hashPasword, phoneNumberCheck, sanitizeString } from '../../../tools/utils';
7+
import { Campaign } from '../../../Models/Campaign';
78

89
/**
910
* Create a new caller
@@ -78,11 +79,13 @@ export default async function newCaller(req: Request<any>, res: Response<any>) {
7879
return;
7980
}
8081

82+
const activeCampaigns = await Campaign.find({ area: area._id, active: true });
83+
8184
const newCaller = new Caller({
8285
name: sanitizeString(req.body.name),
8386
phone: phone,
8487
pinCode: req.body.pinCode,
85-
area: area._id
88+
campaigns: activeCampaigns.map(campaign => campaign._id)
8689
});
8790

8891
const result = await newCaller.save();

router/admin/caller/removeCaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default async function removeCaller(req: Request<any>, res: Response<any>
6060
return;
6161
}
6262

63-
const caller = await Caller.findOne({ phone: phone });
63+
const caller = await Caller.findOne({ phone: phone, campaigns: { $in: area.campaignList } });
6464
if (!caller) {
6565
res.status(400).send({ message: 'Caller not found', OK: false });
6666
log(`[${ip}, ${req.body.area}] Caller not found`, 'WARNING', __filename);

router/admin/caller/searchByName.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default async function SearchByName(req: Request<any>, res: Response<any>
5959
const escapedNameParts = sanitizeString(req.body.name).split(' ').map(escapeRegExp);
6060
const regexParts = escapedNameParts.map(part => `(?=.*${part})`).join('');
6161
const regex = new RegExp(`^${regexParts}`, 'i');
62-
const output = await Caller.find({ name: regex, area: area }).limit(10);
62+
const output = await Caller.find({ name: regex, campaigns: { $in: area.campaignList } }).limit(10);
6363

6464
res.status(200).send({ message: 'OK', OK: true, data: output });
6565
log(`[${ip}, ${req.body.area}] Caller searched`, 'INFO', __filename);

0 commit comments

Comments
 (0)