From bff75cfb5400d24c40690030e8d28bc8a5142627 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sun, 14 Jul 2024 17:46:34 -0400 Subject: [PATCH 01/10] feat: auto assign league on register --- functions/userAuth.js | 73 ++++++++++++++++++++++++++++++------------- tsconfig.json | 4 +-- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index 9c58b53d..2cfe4933 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -1,29 +1,60 @@ -const { ID } = require("appwrite"); -const sdk = require("node-appwrite") +// Copyright (c) Gridiron Survivor. +// Licensed under the MIT License. -const user = async ({ req, res, log, error }) => { +import { ID } from 'appwrite'; +import sdk from 'node-appwrite'; +import { Collection } from '@/api/apiFunctions.enum'; - // Init SDK - const client = new sdk.Client(); +/** + * creates a new user record in the User collection + * @param param - The parameters + * @param param.req - The request object + * @param param.res - The response object + * @returns {void} + */ +const user = async ({ req, res }) => { + // Init SDK + const client = new sdk.Client(); - const databases = new sdk.Databases(client); + const databases = new sdk.Databases(client); - client - .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint - .setProject(process.env.PROJECT_ID) // Your project ID - .setKey(process.env.X_Appwrite_Key) // Your secret API key + client + .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint + .setProject(process.env.PROJECT_ID) // Your project ID + .setKey(process.env.X_Appwrite_Key); // Your secret API key - // create a new user - if (req.method === "POST" && req.body.email && req.body.email && req.body['$id']) { - await databases.createDocument(process.env.DATABASE_ID, process.env.COLLECTION_USERS_ID, ID.unique(), { - "email": req.body.email, - "name": req.body.name, - "labels": req.body.labels, - "userId": req.body['$id'] - }); - return res.json({ msg: "User was created successfully!" }); - } + // create a new user + if ( + req.method === 'POST' && + req.body.email && + req.body.email && + req.body['$id'] + ) { + await databases.createDocument( + process.env.DATABASE_ID, + process.env.COLLECTION_USERS_ID, + ID.unique(), + { + email: req.body.email, + name: req.body.name, + labels: req.body.labels, + userId: req.body['$id'], + leagues: ['66311a210039f0532044'], + }, + ); + await databases.updateDocument( + process.env.DATABASE_ID, + Collection.LEAGUE, + '66311a210039f0532044', + { + participants: [req.body['$id']], + survivors: [req.body['$id']], + }, + ); + + return res.json({ msg: 'User was created successfully!' }); + } }; -module.exports = user; \ No newline at end of file +module.exports = user; diff --git a/tsconfig.json b/tsconfig.json index f8f40260..744e3ef8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -30,6 +30,6 @@ "**/*.tsx", ".next/types/**/*.ts", "jest.config.ts" - ], +, "functions/userAuth.js" ], "exclude": ["node_modules"] -} \ No newline at end of file +} From 7f736ac504088ecb9e19fd208cecef7605cf2e3a Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sun, 14 Jul 2024 17:59:53 -0400 Subject: [PATCH 02/10] fix: update function to include current list of participants and survivors --- functions/userAuth.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index 2cfe4933..ed537fd2 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -39,17 +39,27 @@ const user = async ({ req, res }) => { name: req.body.name, labels: req.body.labels, userId: req.body['$id'], - leagues: ['66311a210039f0532044'], + leagues: '66311a210039f0532044', }, ); + // get the list of participants and survivors from the league + const league = await databases.getDocument( + process.env.DATABASE_ID, + Collection.LEAGUE, + '66311a210039f0532044', + ); + + const updatedParticipants = [...league.participants, req.body['$id']]; + const updatedSurvivors = [...league.survivors, req.body['$id']]; + await databases.updateDocument( process.env.DATABASE_ID, Collection.LEAGUE, '66311a210039f0532044', { - participants: [req.body['$id']], - survivors: [req.body['$id']], + participants: updatedParticipants, + survivors: updatedSurvivors, }, ); From fcaa20b800cb8eaace917790c38842891507e034 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sun, 14 Jul 2024 18:05:23 -0400 Subject: [PATCH 03/10] :fix: import statement --- functions/userAuth.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index ed537fd2..9a93300b 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -1,8 +1,8 @@ // Copyright (c) Gridiron Survivor. // Licensed under the MIT License. -import { ID } from 'appwrite'; -import sdk from 'node-appwrite'; +const { ID } = require('appwrite'); +const sdk = require('node-appwrite'); import { Collection } from '@/api/apiFunctions.enum'; /** From 94f76e1493de68bf6a47c5cab9ab19aaf1fe3fe7 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sun, 14 Jul 2024 18:09:04 -0400 Subject: [PATCH 04/10] fix: import statement --- functions/userAuth.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index 9a93300b..16c11935 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -3,7 +3,6 @@ const { ID } = require('appwrite'); const sdk = require('node-appwrite'); -import { Collection } from '@/api/apiFunctions.enum'; /** * creates a new user record in the User collection @@ -46,7 +45,7 @@ const user = async ({ req, res }) => { // get the list of participants and survivors from the league const league = await databases.getDocument( process.env.DATABASE_ID, - Collection.LEAGUE, + '6626a937b6302f6a4d28', '66311a210039f0532044', ); @@ -55,7 +54,7 @@ const user = async ({ req, res }) => { await databases.updateDocument( process.env.DATABASE_ID, - Collection.LEAGUE, + '6626a937b6302f6a4d28', '66311a210039f0532044', { participants: updatedParticipants, From c21b5e2e28de335acf15c3997836125f89acb118 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sun, 14 Jul 2024 18:15:20 -0400 Subject: [PATCH 05/10] fix: return statement --- functions/userAuth.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index 16c11935..aee95df8 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -38,7 +38,7 @@ const user = async ({ req, res }) => { name: req.body.name, labels: req.body.labels, userId: req.body['$id'], - leagues: '66311a210039f0532044', + leagues: ['66311a210039f0532044'], }, ); @@ -52,7 +52,7 @@ const user = async ({ req, res }) => { const updatedParticipants = [...league.participants, req.body['$id']]; const updatedSurvivors = [...league.survivors, req.body['$id']]; - await databases.updateDocument( + const updateLeague = await databases.updateDocument( process.env.DATABASE_ID, '6626a937b6302f6a4d28', '66311a210039f0532044', @@ -62,7 +62,8 @@ const user = async ({ req, res }) => { }, ); - return res.json({ msg: 'User was created successfully!' }); + // return res.json({ msg: 'User was created successfully!' }); + return res.json(league, updateLeague); } }; From dc976cb2c4d210679f699e84c435fc95dddc461f Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sun, 14 Jul 2024 18:28:22 -0400 Subject: [PATCH 06/10] fix(#355): temp auto add new registered user to gis league --- functions/userAuth.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index aee95df8..fa4b7173 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -49,10 +49,12 @@ const user = async ({ req, res }) => { '66311a210039f0532044', ); + // add the new user id to the list of participants and survivors const updatedParticipants = [...league.participants, req.body['$id']]; const updatedSurvivors = [...league.survivors, req.body['$id']]; - const updateLeague = await databases.updateDocument( + // update the league with the new users id into the list of participants and survivors + await databases.updateDocument( process.env.DATABASE_ID, '6626a937b6302f6a4d28', '66311a210039f0532044', @@ -62,8 +64,7 @@ const user = async ({ req, res }) => { }, ); - // return res.json({ msg: 'User was created successfully!' }); - return res.json(league, updateLeague); + return res.json({ msg: 'User was created successfully!' }); } }; From 24dd9c7f488234abc0f8b485b01eb86c11453641 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sun, 14 Jul 2024 18:34:03 -0400 Subject: [PATCH 07/10] update tsconfig to develop branch version --- tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 744e3ef8..f8f40260 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -30,6 +30,6 @@ "**/*.tsx", ".next/types/**/*.ts", "jest.config.ts" -, "functions/userAuth.js" ], + ], "exclude": ["node_modules"] -} +} \ No newline at end of file From 72090379a693997996b553f5b967fb75d343509b Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:41:46 -0400 Subject: [PATCH 08/10] fix: update if statement to check for duplicate email --- functions/userAuth.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index fa4b7173..9e00b951 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -23,12 +23,7 @@ const user = async ({ req, res }) => { .setKey(process.env.X_Appwrite_Key); // Your secret API key // create a new user - if ( - req.method === 'POST' && - req.body.email && - req.body.email && - req.body['$id'] - ) { + if (req.method === 'POST' && req.body.email && req.body['$id']) { await databases.createDocument( process.env.DATABASE_ID, process.env.COLLECTION_USERS_ID, From ab30d35bab4ccdedb6d04b9ef3720ffaf94453d7 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:11:20 -0400 Subject: [PATCH 09/10] fix: update function for magicURLs --- functions/userAuth.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index 9e00b951..613faeda 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -1,8 +1,8 @@ // Copyright (c) Gridiron Survivor. // Licensed under the MIT License. -const { ID } = require('appwrite'); -const sdk = require('node-appwrite'); +const { ID, Query } = require('appwrite'); +const sdk, = require('node-appwrite'); /** * creates a new user record in the User collection @@ -22,6 +22,17 @@ const user = async ({ req, res }) => { .setProject(process.env.PROJECT_ID) // Your project ID .setKey(process.env.X_Appwrite_Key); // Your secret API key + // check if user exists + const user = await databases.listDocuments( + process.env.DATABASE_ID, + process.env.COLLECTION_USERS_ID, + [Query.equal('$id', req.body['$id'])], + ); + + if (user.documents.length > 0) { + return res.json({ msg: 'User already exists!' }); + } + // create a new user if (req.method === 'POST' && req.body.email && req.body['$id']) { await databases.createDocument( From eeca13cb71f89029aff291277795c8803974f911 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:42:24 -0400 Subject: [PATCH 10/10] fix: function --- functions/userAuth.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/functions/userAuth.js b/functions/userAuth.js index 613faeda..0b672b44 100644 --- a/functions/userAuth.js +++ b/functions/userAuth.js @@ -1,8 +1,8 @@ // Copyright (c) Gridiron Survivor. // Licensed under the MIT License. -const { ID, Query } = require('appwrite'); -const sdk, = require('node-appwrite'); +const { ID } = require('appwrite'); +const sdk = require('node-appwrite'); /** * creates a new user record in the User collection @@ -14,7 +14,6 @@ const sdk, = require('node-appwrite'); const user = async ({ req, res }) => { // Init SDK const client = new sdk.Client(); - const databases = new sdk.Databases(client); client @@ -22,17 +21,6 @@ const user = async ({ req, res }) => { .setProject(process.env.PROJECT_ID) // Your project ID .setKey(process.env.X_Appwrite_Key); // Your secret API key - // check if user exists - const user = await databases.listDocuments( - process.env.DATABASE_ID, - process.env.COLLECTION_USERS_ID, - [Query.equal('$id', req.body['$id'])], - ); - - if (user.documents.length > 0) { - return res.json({ msg: 'User already exists!' }); - } - // create a new user if (req.method === 'POST' && req.body.email && req.body['$id']) { await databases.createDocument(