Skip to content

Commit ef9f7c7

Browse files
pierreTkleinchenxuan-zhoujamesxu123
authored
feat: settings in seed (#682)
* refactor: move const settings objects into constants file * feat: add settings to seed script * fix: update settings schema defaults so that they are more sensible, and use that in seed * fix: export and use settings constants * fix: import setting constants directly in test Co-authored-by: chenxuan-zhou <46543122+chenxuan-zhou@users.noreply.github.com> Co-authored-by: James Xu <xujames007@gmail.com>
1 parent e6f721e commit ef9f7c7

File tree

7 files changed

+90
-59
lines changed

7 files changed

+90
-59
lines changed

constants/settings.constant.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const APP_NOT_YET_OPEN = {
2+
openTime: new Date(Date.now() + 100000000000),
3+
closeTime: new Date(Date.now() + 10000000000000000),
4+
confirmTime: new Date(Date.now() + 100000000000000000)
5+
};
6+
7+
const APP_OPEN = {
8+
openTime: new Date(Date.now() - 100),
9+
closeTime: new Date(Date.now() + 10000000000),
10+
confirmTime: new Date(Date.now() + 100000000000000)
11+
};
12+
13+
const APP_CLOSED = {
14+
openTime: new Date(Date.now() - 100),
15+
closeTime: new Date(Date.now() - 1000),
16+
confirmTime: new Date(Date.now() + 100000000000000)
17+
};
18+
19+
const CONFIRM_CLOSED = {
20+
openTime: new Date(Date.now() - 10000),
21+
closeTime: new Date(Date.now() - 1000),
22+
confirmTime: new Date(Date.now() - 100)
23+
};
24+
25+
const REMOTE_HACKATHON = {
26+
openTime: new Date(Date.now() - 100),
27+
closeTime: new Date(Date.now() + 10000000000),
28+
confirmTime: new Date(Date.now() + 100000000000000),
29+
isRemote: true
30+
};
31+
32+
// Some utility dates that are used in tests and seed script
33+
module.exports = {
34+
APP_NOT_YET_OPEN: APP_NOT_YET_OPEN,
35+
APP_OPEN: APP_OPEN,
36+
APP_CLOSED: APP_CLOSED,
37+
CONFIRM_CLOSED: CONFIRM_CLOSED,
38+
REMOTE_HACKATHON: REMOTE_HACKATHON
39+
};

models/settings.model.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ const mongoose = require("mongoose");
44
const settings = new mongoose.Schema({
55
openTime: {
66
type: Date,
7-
default: 0
7+
default: Date.now() + 2628000000 // One month from now.
88
},
99
closeTime: {
1010
type: Date,
11-
default: Date.now() + 31104000000 // Add a year from now.
11+
default: Date.now() + 31540000000 + 2628000000 // One year and 1 month from now.
1212
},
1313
confirmTime: {
1414
type: Date,
15-
default: Date.now() + 31104000000 + 2628000000 // 1 year and 1 month from now.
15+
default: Date.now() + 31540000000 + 2628000000 + 2628000000 // 1 year and 2 months from now.
1616
},
1717
isRemote: {
1818
type: Boolean,

seed/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
"use strict";
22
const Constants = {
3-
Role: require("../constants/role.constant")
3+
Role: require("../constants/role.constant"),
4+
Settings: require("../constants/settings.constant")
45
};
56

67
const Seed = {
7-
Roles: require("./roles.seed")
8+
Roles: require("./roles.seed"),
9+
Settings: require("./settings.seed")
810
};
911

12+
const Services = {
13+
env: require("../services/env.service")
14+
};
15+
const path = require("path");
16+
17+
const envLoadResult = Services.env.load(path.join(__dirname, "../.env"));
18+
if (envLoadResult.error) {
19+
Services.log.error(envLoadResult.error);
20+
}
21+
1022
const db = require("../services/database.service");
1123
//connect to db
1224
db.connect(undefined, () => {
@@ -30,8 +42,10 @@ async function onConnected() {
3042

3143
async function dropAll() {
3244
await Seed.Roles.dropAll();
45+
await Seed.Settings.drop();
3346
}
3447

3548
async function storeAll() {
3649
await Seed.Roles.storeAll(Constants.Role.allRolesArray);
50+
await Seed.Settings.store();
3751
}

seed/roles.seed.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
"use strict";
22
const Role = require("../models/role.model");
3-
const Services = {
4-
env: require("../services/env.service")
5-
};
6-
const path = require("path");
7-
8-
const envLoadResult = Services.env.load(path.join(__dirname, "../.env"));
9-
if (envLoadResult.error) {
10-
Services.log.error(envLoadResult.error);
11-
}
123

134
/**
145
* Drops all elements in Role

seed/settings.seed.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
const Settings = require("../models/settings.model");
3+
4+
/**
5+
* Drops all elements in Role
6+
*/
7+
function drop() {
8+
return Settings.deleteMany({});
9+
}
10+
11+
/**
12+
* Stores all of the roles in the db
13+
* @param {Settings} setting the setting that we want to seed
14+
*/
15+
function store(setting) {
16+
return Settings.collection.insertOne(new Settings(setting));
17+
}
18+
19+
module.exports = {
20+
store: store,
21+
drop: drop
22+
};

tests/settings.test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ chai.should();
88
const util = {
99
account: require("./util/account.test.util"),
1010
auth: require("./util/auth.test.util"),
11-
settings: require("./util/settings.test.util")
1211
};
1312

1413
const Constants = {
1514
Success: require("../constants/success.constant"),
16-
Error: require("../constants/error.constant")
15+
Error: require("../constants/error.constant"),
16+
Settings: require("../constants/settings.constant")
1717
};
1818

1919
const invalidAccount = util.account.hackerAccounts.stored.noTeam[0];
@@ -85,8 +85,7 @@ describe("PATCH settings", function() {
8585
agent
8686
.patch(`/api/settings/`)
8787
.type("application/json")
88-
.send(util.settings.settingConfirmClosed)
89-
// does not have password because of to stripped json
88+
.send(Constants.Settings.CONFIRM_CLOSED)
9089
.end(function(err, res) {
9190
res.should.have.status(200);
9291
res.should.be.json;
@@ -109,8 +108,7 @@ describe("PATCH settings", function() {
109108
agent
110109
.patch(`/api/settings/`)
111110
.type("application/json")
112-
.send(util.settings.settingRemoteHackathon)
113-
// does not have password because of to stripped json
111+
.send(Constants.Settings.REMOTE_HACKATHON)
114112
.end(function(err, res) {
115113
res.should.have.status(200);
116114
res.should.be.json;

tests/util/settings.test.util.js

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,23 @@
11
const Settings = require("../../models/settings.model");
22
const logger = require("../../services/logger.service");
3-
4-
const settingApplicationNotYetOpen = {
5-
openTime: new Date(Date.now() + 100000000000),
6-
closeTime: new Date(Date.now() + 10000000000000000),
7-
confirmTime: new Date(Date.now() + 100000000000000000)
8-
};
9-
10-
const settingApplicationOpen = {
11-
openTime: new Date(Date.now() - 100),
12-
closeTime: new Date(Date.now() + 10000000000),
13-
confirmTime: new Date(Date.now() + 100000000000000)
14-
};
15-
16-
const settingApplicationClosed = {
17-
openTime: new Date(Date.now() - 100),
18-
closeTime: new Date(Date.now() - 1000),
19-
confirmTime: new Date(Date.now() + 100000000000000)
20-
};
21-
22-
const settingConfirmClosed = {
23-
openTime: new Date(Date.now() - 10000),
24-
closeTime: new Date(Date.now() - 1000),
25-
confirmTime: new Date(Date.now() - 100)
26-
};
27-
28-
const settingRemoteHackathon = {
29-
openTime: new Date(Date.now() - 100),
30-
closeTime: new Date(Date.now() + 10000000000),
31-
confirmTime: new Date(Date.now() + 100000000000000),
32-
isRemote: true
3+
const Constants = {
4+
Settings: require("../../constants/settings.constant")
335
};
346

357
async function storeAll() {
36-
const toStore = new Settings(settingApplicationOpen);
8+
const toStore = new Settings(Constants.Settings.APP_OPEN);
379
Settings.collection.insertOne(toStore);
3810
}
3911

4012
async function setApplicationClosed() {
4113
await dropAll();
42-
const toStore = new Settings(settingApplicationClosed);
14+
const toStore = new Settings(Constants.Settings.APP_CLOSED);
4315
Settings.collection.insertOne(toStore);
4416
}
4517

4618
async function setApplicationNotYetOpen() {
4719
await dropAll();
48-
const toStore = new Settings(settingApplicationNotYetOpen);
20+
const toStore = new Settings(Constants.Settings.APP_NOT_YET_OPEN);
4921
Settings.collection.insertOne(toStore);
5022
}
5123

@@ -64,10 +36,5 @@ module.exports = {
6436
storeAll: storeAll,
6537
dropAll: dropAll,
6638
setApplicationClosed: setApplicationClosed,
67-
setApplicationNotYetOpen: setApplicationNotYetOpen,
68-
settingApplicationNotYetOpen: settingApplicationNotYetOpen,
69-
settingApplicationOpen: settingApplicationOpen,
70-
settingApplicationClosed: settingApplicationClosed,
71-
settingConfirmClosed: settingConfirmClosed,
72-
settingRemoteHackathon: settingRemoteHackathon
39+
setApplicationNotYetOpen: setApplicationNotYetOpen
7340
};

0 commit comments

Comments
 (0)