Skip to content

Commit

Permalink
#355 - temp auto assign GIS league on register (#404)
Browse files Browse the repository at this point in the history
fixes: #355 

Temporarily auto assign new registered users to the GiS league.
  • Loading branch information
chris-nowicki authored Jul 16, 2024
1 parent 6f9356a commit 777b62a
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions functions/userAuth.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
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 }) => {
const { ID } = require('appwrite');
const sdk = require('node-appwrite');

// 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['$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'],
},
);

// 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!" });
}
// get the list of participants and survivors from the league
const league = await databases.getDocument(
process.env.DATABASE_ID,
'6626a937b6302f6a4d28',
'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']];

// update the league with the new users id into the list of participants and survivors
await databases.updateDocument(
process.env.DATABASE_ID,
'6626a937b6302f6a4d28',
'66311a210039f0532044',
{
participants: updatedParticipants,
survivors: updatedSurvivors,
},
);

return res.json({ msg: 'User was created successfully!' });
}
};

module.exports = user;
module.exports = user;

0 comments on commit 777b62a

Please sign in to comment.