From e5bc39043d140e4e3f053b675fe40cb800da9e83 Mon Sep 17 00:00:00 2001 From: Sendou Date: Tue, 7 Jan 2020 17:24:32 +0200 Subject: [PATCH] addVouch backend first version --- graphql-schemas/plus.js | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/graphql-schemas/plus.js b/graphql-schemas/plus.js index b7e085d31..c4a7f1a39 100644 --- a/graphql-schemas/plus.js +++ b/graphql-schemas/plus.js @@ -26,6 +26,7 @@ const typeDef = gql` region: String! description: String! ): Boolean! + addVouch(discord_id: String!, server: String!, region: String!): Boolean! addVotes(votes: [VoteInput!]!): Boolean! startVoting(ends: String!): Boolean! endVoting: Boolean! @@ -378,6 +379,56 @@ const resolvers = { return true }, + addVouch: async (root, args, ctx) => { + /*addVouch( + discord_id: String! + server: String! + region: String! + ): Boolean! */ + if (!ctx.user) throw new AuthenticationError("Not logged in.") + if (!ctx.user.plus || !ctx.user.plus.membership_status) { + throw new AuthenticationError("Not plus member.") + } + + if (args.server !== "ONE" && args.server !== "TWO") + throw new UserInputError("Invalid plus server given.") + if (args.region !== "EU" && args.region !== "NA") + throw new UserInputError("Invalid region given.") + + const can_vouch = !ctx.user.plus.can_vouch + if (!can_vouch || (can_vouch !== "ONE" && args.server === "ONE")) + throw new UserInputError("No privileges to vouch.") + + if (ctx.user.plus.can_vouch_again_after) + throw new UserInputError( + "No privileges to vouch right now due to previous vouch getting kicked." + ) + + const user = await User.findOne({ discord_id: args.discord_id }) + + if (!user) + throw new UserInputError("User vouched is not a sendou.ink member.") + + if ( + user.plus && + (user.plus.membership_status === args.server || + user.plus.vouch_status === args.server || + user.plus.membership_status === "ONE" || + user.plus.vouch_status === "ONE") + ) + throw new UserInputError("User already has access.") + + if (!user.plus) user.plus = {} + user.plus.vouch_status = args.server + user.plus.voucher_discord_id = ctx.user.discord_id + if (!user.plus.plus_region) user.plus.plus_region = args.region + ctx.user.plus.can_vouch = null + + await user.save() + await ctx.user.save() //works or nah? + + return true + }, addVotes: async (root, args, ctx) => { if (!ctx.user) throw new AuthenticationError("Not logged in.") if (!ctx.user.plus || !ctx.user.plus.membership_status) {