Skip to content

Commit

Permalink
change to post route for register route
Browse files Browse the repository at this point in the history
  • Loading branch information
pogi7 committed Oct 28, 2023
1 parent c1b7e82 commit 11a5398
Show file tree
Hide file tree
Showing 3 changed files with 747 additions and 51 deletions.
3 changes: 0 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ services:
ports:
- "5000:5000"
env_file: "src/account/.env"
extra_hosts:
- "host.docker.internal:host-gateway"
dns:
- "8.8.8.8"
network_mode: "host"
# client:
# build: "client/"
# command: "npm start"
Expand Down
97 changes: 49 additions & 48 deletions src/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,64 @@ export const router = express.Router();
export const MONGO_CLIENT = new MongoClient(URL);

// Connect to the Atlas cluster
async function connect() {
export async function connect(): Promise<boolean> {
try {
await MONGO_CLIENT.connect();
console.log("Successfully connected to MongoDB.");
return true;
} catch (err) {
console.error("Error connecting to MongoDB " + err);
return false;
}
}

// Register method
router.get("/register", (req: any, res: any) => {
async function registerAccount() {
try {
// Create a new document
let accountDocument: IUserSchema = {
creationDate: {
value: Date.now()
},
deletionDate: {
value: Date.now()
},
lastUpdated: {
value: Date.now()
},
email: {
value: req.body.email,
},
name: {
value: req.body.name,
},
devicesLinked: {
value: {}
}
};

// Pick database to connect to with mongo client
const mongo_db = MONGO_CLIENT.db(DB);

// Reference the "accounts" collection in the specified database
const col = mongo_db.collection(COL);

// Insert the document into the specified collection
await col.insertOne(accountDocument);
console.log("Account registered successfully");

// TODO: Use this code for unit tests
// // Find and return the document
// const filter = { "name.last": "Turing" };
// const document = await col.findOne(filter);
// console.log("Document found:\n" + JSON.stringify(document));
} catch (err: any) {
console.log("Error " + err.stack);
console.log("Response " + res);
}
export async function registerAccount(req: any, res: any): Promise<void> {
try {
// Create a new document
let accountDocument: IUserSchema = {
creationDate: {
value: Date.now()
},
deletionDate: {
value: Date.now()
},
lastUpdated: {
value: Date.now()
},
email: {
value: req.body.email,
},
name: {
value: req.body.name,
},
devicesLinked: {
value: {}
}
};

// Pick database to connect to with mongo client
const mongo_db = MONGO_CLIENT.db(DB);

// Reference the "accounts" collection in the specified database
const col = mongo_db.collection(COL);

// Insert the document into the specified collection
await col.insertOne(accountDocument);
console.log("Account registered successfully");

// TODO: Use this code for unit tests
// // Find and return the document
// const filter = { "name.last": "Turing" };
// const document = await col.findOne(filter);
// console.log("Document found:\n" + JSON.stringify(document));
} catch (err: any) {
console.log("Error " + err.stack);
console.log("Response " + res);
}
registerAccount().catch(console.dir);
});
}

// Register route
router.post("/register", registerAccount);

// Get all method

Expand Down
Loading

0 comments on commit 11a5398

Please sign in to comment.