Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#355 - temp auto assign GIS league on register #404

Merged
merged 12 commits into from
Jul 16, 2024
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',
vmaineng marked this conversation as resolved.
Show resolved Hide resolved
{
participants: updatedParticipants,
survivors: updatedSurvivors,
},
);

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

module.exports = user;
module.exports = user;
vmaineng marked this conversation as resolved.
Show resolved Hide resolved
Loading