-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate.js
42 lines (40 loc) · 1.29 KB
/
activate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const { transfer } = require('./transfer');
const { sendEmail } = require('../../../comms');
module.exports = {
activate: async (hash, model, nextModel, storageModel) => {
let confirmation = {
isHashConfirmed: true,
isLinkExpired: false,
};
const userExists = await model.countDocuments({
joinHash: { $eq: hash },
}),
verified = Boolean(userExists);
if (!verified) return (confirmation.isHashConfirmed = false);
if (verified) {
const matchingUser = await model.findOne({
joinHash: { $eq: hash },
});
if (!matchingUser.joinHashExpires) return; // is this necessary if collection has same TTL in db index?
try {
if (matchingUser.joinHash) {
await transfer(matchingUser, nextModel);
const recordOfExUser = await storageModel.findOne({
username: { $eq: matchingUser.username },
});
if (recordOfExUser) {
return await recordOfExUser.delete();
}
await sendEmail(matchingUser.username, null, 'JoinedUp', null);
await matchingUser.remove();
return confirmation;
} else {
confirmation.isHashConfirmed = false;
return confirmation;
}
} catch (err) {
return err;
}
}
},
};