Skip to content

Commit

Permalink
add evalPopulate()
Browse files Browse the repository at this point in the history
  • Loading branch information
sssomeshhh committed May 28, 2024
1 parent 0c32fdc commit 5032c2a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
71 changes: 71 additions & 0 deletions be/src/helpers/populate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {bcr} from "./imports.js";
import { Property, User } from "./schemas.js";

const evalPopulate = async () => {
let sellerId;
await User.create({
firstName: 'Seller',
lastName: 'User',
email: '[email protected]',
phoneNumber: '97531',
password: await bcr.hash('Seller.User.97531', 10),
role: 'seller'
}).then((seller) => {
sellerId = seller._id.toString();
});
await User.create({
firstName: 'Buyer',
lastName: 'User',
email: '[email protected]',
phoneNumber: '86420',
password: await bcr.hash('Buyer.User.86420', 10),
role: 'buyer'
});
await Property.create({
title: 'P1.Title',
description: 'P1.Description',
location: 'L.A',
bedrooms: 1,
bathrooms: 1,
rent: 1000,
seller: sellerId
});
await Property.create({
title: 'P2.Title',
description: 'P2.Description',
location: 'L.B',
bedrooms: 2,
bathrooms: 2,
rent: 2000,
seller: sellerId
});
await Property.create({
title: 'P3.Title',
description: 'P3.Description',
location: 'L.C',
bedrooms: 3,
bathrooms: 3,
rent: 3000,
seller: sellerId
});
await Property.create({
title: 'P4.Title',
description: 'P4.Description',
location: 'L.D',
bedrooms: 4,
bathrooms: 4,
rent: 4000,
seller: sellerId
});
await Property.create({
title: 'P5.Title',
description: 'P5.Description',
location: 'L.E',
bedrooms: 5,
bathrooms: 5,
rent: 5000,
seller: sellerId
});
}

export { evalPopulate };
11 changes: 10 additions & 1 deletion be/src/helpers/server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { crs, exp, pth, url } from "./imports.js";

import { connectDatabase } from "./database.js";
import { evalPopulate } from "./populate.js";
import { apiRouter } from "./routers.js";

const startServer = () => {
const app = exp();

const isEval = process.env.IS_EVAL;

const serverPort = process.env.SERVER_PORT;

const staticDir = pth.join(pth.dirname(url.fileURLToPath(import.meta.url)), "../../../fe/build");
Expand All @@ -32,7 +35,13 @@ const startServer = () => {
res.sendFile(`${staticDir}/index.html`)
});

connectDatabase().then(() => {});
connectDatabase().then(() => {
if (isEval === 'true') {
evalPopulate().then(() => {
console.log('[server] Populated database with sample data for evaluation');
});
}
});

app.listen(serverPort, () => {
console.log(`[server] Listening on port ${serverPort}`);
Expand Down

0 comments on commit 5032c2a

Please sign in to comment.