2020-08-26 01:19:09 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-08-28 07:34:50 +03:00
|
|
|
import * as Serializers from "~/node_common/serializers";
|
2020-08-26 01:19:09 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
if (!id) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_TRUST", error: true });
|
2020-08-26 01:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(404).send({
|
2020-08-26 01:19:09 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 01:19:09 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 06:22:41 +03:00
|
|
|
if (!req.body.data || !req.body.data.userId) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 06:22:41 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_MUST_PROVIDE_SOMEONE_TO_TRUST",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.id === req.body.data.userId) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 06:22:41 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_CAN_NOT_TRUST_YOURSELF",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetUser = await Data.getUserById({
|
|
|
|
id: req.body.data.userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!targetUser) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(404).send({
|
2020-08-26 06:22:41 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_TARGET_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetUser.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 06:22:41 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_TARGET_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 13:03:39 +03:00
|
|
|
let existingResponse = await Data.getTrustedRelationshipByUserIds({
|
2020-08-26 01:19:09 +03:00
|
|
|
ownerUserId: user.id,
|
2020-08-26 06:22:41 +03:00
|
|
|
targetUserId: targetUser.id,
|
2020-08-26 01:19:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (existingResponse && existingResponse.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 01:19:09 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_CHECK_ERROR",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 13:03:39 +03:00
|
|
|
let invertedResponse = await Data.getTrustedRelationshipByUserIds({
|
|
|
|
targetUserId: user.id,
|
|
|
|
ownerUserId: targetUser.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (invertedResponse) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 13:03:39 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_INVERTED_CHECK_ERROR",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 01:19:09 +03:00
|
|
|
// NOTE(jim)
|
|
|
|
// Treat trust as an API method you can call again to remove a trusted relationship.
|
|
|
|
if (existingResponse) {
|
|
|
|
const deleteRelationshipResponse = await Data.deleteTrustedRelationshipById(
|
|
|
|
{
|
|
|
|
id: existingResponse.id,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!deleteRelationshipResponse) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(404).send({
|
2020-08-26 01:19:09 +03:00
|
|
|
decorator: "SERVER_DELETE_TRUSTED_RELATIONSHIP_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deleteRelationshipResponse.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 01:19:09 +03:00
|
|
|
decorator: "SERVER_DELETE_TRUSTED_RELATIONSHIP_ERROR",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({
|
2020-08-26 01:19:09 +03:00
|
|
|
decorator: "SERVER_DELETE_TRUSTED_RELATIONSHIP",
|
|
|
|
data: deleteRelationshipResponse,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const trustResponse = await Data.createTrustedRelationship({
|
|
|
|
ownerUserId: user.id,
|
2020-08-26 06:32:40 +03:00
|
|
|
targetUserId: targetUser.id,
|
2020-08-26 01:19:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!trustResponse) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(404).send({
|
2020-08-26 01:19:09 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trustResponse.error) {
|
|
|
|
return res
|
|
|
|
.status(500)
|
2020-09-03 03:21:29 +03:00
|
|
|
.send({ decorator: "SERVER_TRUSTED_RELATIONSHIP_ERROR", error: true });
|
2020-08-26 01:19:09 +03:00
|
|
|
}
|
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({
|
2020-08-28 07:34:50 +03:00
|
|
|
decorator: "SERVER_TRUSTED_RELATIONSHIP",
|
|
|
|
data: {
|
|
|
|
...trustResponse,
|
|
|
|
owner: Serializers.user(user),
|
|
|
|
user: Serializers.user(targetUser),
|
|
|
|
},
|
|
|
|
});
|
2020-08-26 01:19:09 +03:00
|
|
|
};
|