mirror of
https://github.com/status-im/contract-notifier.git
synced 2025-02-24 00:48:16 +00:00
Error handling for DB
This commit is contained in:
parent
74378d2a8d
commit
5b85a69d2d
@ -27,48 +27,51 @@ class Controller {
|
|||||||
|
|
||||||
// TODO: handle subscriptions to particular events
|
// TODO: handle subscriptions to particular events
|
||||||
|
|
||||||
const subscriber = await Subscribers.findOne({
|
try {
|
||||||
dappId,
|
const subscriber = await Subscribers.findOne({
|
||||||
address
|
|
||||||
});
|
|
||||||
|
|
||||||
const t = getToken();
|
|
||||||
|
|
||||||
if (!subscriber) {
|
|
||||||
const s = await Subscribers.create({
|
|
||||||
dappId,
|
dappId,
|
||||||
email,
|
|
||||||
address
|
address
|
||||||
});
|
});
|
||||||
|
|
||||||
await Verifications.create({
|
const t = getToken();
|
||||||
...t,
|
|
||||||
subscriber: s._id
|
if (!subscriber) {
|
||||||
});
|
const s = await Subscribers.create({
|
||||||
} else if (!subscriber.isVerified) {
|
dappId,
|
||||||
const d = new Date(subscriber.lastSignUpAttempt);
|
email,
|
||||||
d.setMinutes(d.getMinutes() + 5);
|
address
|
||||||
if (d > new Date()) {
|
});
|
||||||
return next(new BadRequest("You need to wait at least 5 minutes between sign up attempts"));
|
|
||||||
|
await Verifications.create({
|
||||||
|
...t,
|
||||||
|
subscriber: s._id
|
||||||
|
});
|
||||||
|
} else if (!subscriber.isVerified) {
|
||||||
|
const d = new Date(subscriber.lastSignUpAttempt);
|
||||||
|
d.setMinutes(d.getMinutes() + 5);
|
||||||
|
if (d > new Date()) {
|
||||||
|
return next(new BadRequest("You need to wait at least 5 minutes between sign up attempts"));
|
||||||
|
}
|
||||||
|
|
||||||
|
subscriber.lastSignUpAttempt = d;
|
||||||
|
await subscriber.save();
|
||||||
|
|
||||||
|
await Verifications.create({
|
||||||
|
...t,
|
||||||
|
subscriber: subscriber._id
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
subscriber.lastSignUpAttempt = d;
|
if (!subscriber || !subscriber.isVerified) {
|
||||||
await subscriber.save();
|
const template = dappConfig.template(dappId, "sign-up");
|
||||||
|
mailer.send(dappConfig.getEmailTemplate(dappId, template), dappConfig.config(dappId).from, {
|
||||||
await Verifications.create({
|
email,
|
||||||
...t,
|
token: t.token
|
||||||
subscriber: subscriber._id
|
});
|
||||||
});
|
}
|
||||||
|
} catch (err) {
|
||||||
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!subscriber || !subscriber.isVerified) {
|
|
||||||
const template = dappConfig.template(dappId, "sign-up");
|
|
||||||
mailer.send(dappConfig.getEmailTemplate(dappId, template), dappConfig.config(dappId).from, {
|
|
||||||
email,
|
|
||||||
token: t.token
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).send("OK");
|
return res.status(200).send("OK");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -96,10 +99,14 @@ class Controller {
|
|||||||
|
|
||||||
// TODO: handle unsubscribe to particular events
|
// TODO: handle unsubscribe to particular events
|
||||||
|
|
||||||
await Subscribers.deleteOne({
|
try {
|
||||||
dappId,
|
await Subscribers.deleteOne({
|
||||||
address
|
dappId,
|
||||||
});
|
address
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).send("OK");
|
return res.status(200).send("OK");
|
||||||
};
|
};
|
||||||
@ -116,25 +123,29 @@ class Controller {
|
|||||||
return next(new BadRequest(errors.array()));
|
return next(new BadRequest(errors.array()));
|
||||||
}
|
}
|
||||||
|
|
||||||
const verification = await Verifications.findOne({
|
try {
|
||||||
token
|
const verification = await Verifications.findOne({
|
||||||
}).populate("subscriber");
|
token
|
||||||
|
}).populate("subscriber");
|
||||||
|
|
||||||
if (verification) {
|
if (verification) {
|
||||||
if (verification.expirationTime < new Date()) {
|
if (verification.expirationTime < new Date()) {
|
||||||
return next(new BadRequest("Verification token already expired"));
|
return next(new BadRequest("Verification token already expired"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!verification.subscriber.isVerified) {
|
||||||
|
verification.subscriber.isVerified = true;
|
||||||
|
await verification.subscriber.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
await Verifications.deleteMany({
|
||||||
|
subscriber: verification.subscriber._id
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return next(new BadRequest("Invalid verification token"));
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
if (!verification.subscriber.isVerified) {
|
return next(err);
|
||||||
verification.subscriber.isVerified = true;
|
|
||||||
await verification.subscriber.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
await Verifications.deleteMany({
|
|
||||||
subscriber: verification.subscriber._id
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return next(new BadRequest("Invalid verification token"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).send("OK");
|
return res.status(200).send("OK");
|
||||||
@ -152,13 +163,17 @@ class Controller {
|
|||||||
return next(new BadRequest(errors.array()));
|
return next(new BadRequest(errors.array()));
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscriber = await Subscribers.findOne({
|
try {
|
||||||
dappId,
|
const subscriber = await Subscribers.findOne({
|
||||||
address,
|
dappId,
|
||||||
isVerified: true
|
address,
|
||||||
});
|
isVerified: true
|
||||||
|
});
|
||||||
|
|
||||||
return res.status(200).json({ isUser: subscriber ? true : false });
|
return res.status(200).json({ isUser: subscriber ? true : false });
|
||||||
|
} catch (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,9 @@ events.on("db:connected", () => {
|
|||||||
const response = { error: err.message };
|
const response = { error: err.message };
|
||||||
if (err instanceof BadRequest && err.details) {
|
if (err instanceof BadRequest && err.details) {
|
||||||
response.details = err.details;
|
response.details = err.details;
|
||||||
|
} else {
|
||||||
|
console.error(err);
|
||||||
|
response.error = "Service unavailable";
|
||||||
}
|
}
|
||||||
res.status(err.statusCode).json(response);
|
res.status(err.statusCode).json(response);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user