Skip to content

Commit 2dde1e6

Browse files
committed
Backend integration with the frontend
1 parent e43346a commit 2dde1e6

File tree

10 files changed

+125
-42
lines changed

10 files changed

+125
-42
lines changed

app.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ syncDB();
2929

3030
const app = express();
3131

32+
const options = {
33+
setHeaders(res) {
34+
res.set('Access-Control-Allow-Origin', '*');
35+
}
36+
};
37+
3238
// static content
33-
app.use(express.static(path.join(__dirname, 'public')));
39+
app.use(express.static(path.join(__dirname, 'public'), options));
3440

3541
app.use(cors());
3642
app.use(bodyParser.json());
@@ -42,6 +48,7 @@ require('./app-routes')(app);
4248

4349
app.use(passport.initialize());
4450

51+
4552
// The error handler,log error and return 500 error
4653
// eslint-disable-next-line no-unused-vars
4754
app.use((err, req, res, next) => {
@@ -70,7 +77,6 @@ app.use((err, req, res, next) => {
7077
errorResponse.message = 'server error';
7178
}
7279
}
73-
7480
errorResponse.code = status;
7581
res.status(status).json(errorResponse);
7682
});

integration-test-data.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (C) 2018 TopCoder Inc., All Rights Reserved.
3+
*/
4+
5+
/**
6+
* this script is used to init data, it clears all data, then inserts some sample data
7+
* note: it will be drop all tables and data , then create the new tables
8+
*
9+
* @author TCSCODER
10+
* @version 1.0
11+
*/
12+
13+
const co = require('co');
14+
15+
const models = require('@va/models');
16+
const logger = require('./common/logger');
17+
const helper = require('./modules/security/helper');
18+
19+
co(function* () {
20+
// create lookup entities
21+
yield models.BadgeType.create({ name: 'A Hero', iconURL: 'hero' });
22+
yield models.BadgeType.create({ name: 'Bravery', iconURL: 'bravery' });
23+
yield models.BadgeType.create({ name: 'Instructor', iconURL: 'instructor' });
24+
yield models.BadgeType.create({ name: 'Friendship', iconURL: 'friendship' });
25+
yield models.BadgeType.create({ name: 'A Leader', iconURL: 'leader' });
26+
yield models.BadgeType.create({ name: 'Charger', iconURL: 'charger' });
27+
yield models.BadgeType.create({ name: 'Big Heart', iconURL: 'big-heart' });
28+
yield models.Branch.create({ name: 'branch1' });
29+
yield models.Branch.create({ name: 'branch2' });
30+
yield models.Campaign.create({ name: 'campaign1' });
31+
yield models.Campaign.create({ name: 'campaign2' });
32+
yield models.Country.create({ name: 'USD' });
33+
yield models.Country.create({ name: 'CHINA' });
34+
yield models.EventType.create({ name: 'Born', iconURL: 'event Born' });
35+
yield models.EventType.create({ name: 'Wedding', iconURL: 'event Wedding' });
36+
yield models.EventType.create({ name: 'Enlisted', iconURL: 'event Enlisted' });
37+
yield models.EventType.create({ name: 'Lieutenant Promottion', iconURL: 'event Lieutenant Promottion' });
38+
yield models.EventType.create({ name: 'Passed Away', iconURL: 'event Passed Away' });
39+
// create admin
40+
const passwordHash = yield helper.encryptPassword('password');
41+
yield models.User.create({
42+
username: 'admin',
43+
44+
firstName: 'first name 1',
45+
lastName: 'last name 1',
46+
mobile: '123123123',
47+
role: models.modelConstants.UserRoles.Admin,
48+
status: models.modelConstants.UserStatuses.Active,
49+
gender: 'male',
50+
passwordHash,
51+
countryId: 1
52+
});
53+
// create normal user
54+
yield models.User.create({
55+
username: 'user',
56+
57+
firstName: 'first name 2',
58+
lastName: 'last name 2',
59+
mobile: '123125555',
60+
role: models.modelConstants.UserRoles.User,
61+
status: models.modelConstants.UserStatuses.Active,
62+
gender: 'male',
63+
passwordHash,
64+
countryId: 2
65+
});
66+
logger.info('success!');
67+
process.exit(0);
68+
}).catch((err) => {
69+
logger.error(err);
70+
logger.info('got error, program will exit');
71+
process.exit(1);
72+
});

modules/flags/services/FlagService.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const _ = require('lodash');
1212
const models = require('va-online-memorial-data-models');
1313
const logger = require('../../../common/logger');
1414
const helper = require('../../../common/helper');
15+
const PhotoService = require('../../photos/services/PhotoService');
16+
const BadgeService = require('../../badges/services/BadgeService');
1517

1618
/**
1719
* build db search query
@@ -86,13 +88,13 @@ function* validatePost(postId, postType) {
8688
function* populatePost(f) {
8789
if (!f) return f;
8890
if (f.postType === models.modelConstants.PostTypes.Story) {
89-
f.story = yield helper.ensureExists(models.Story, { id: f.postId });
91+
f.story = yield helper.populateUsersForEntity(yield helper.ensureExists(models.Story, { id: f.postId }));
9092
} else if (f.postType === models.modelConstants.PostTypes.Photo) {
91-
f.photo = yield helper.ensureExists(models.Photo, { id: f.postId });
93+
f.photo = yield PhotoService.getSingle(f.postId);
9294
} else if (f.postType === models.modelConstants.PostTypes.Badge) {
93-
f.badge = yield helper.ensureExists(models.Badge, { id: f.postId });
95+
f.badge = yield BadgeService.getSingle(f.postId);
9496
} else if (f.postType === models.modelConstants.PostTypes.Testimonial) {
95-
f.testimonial = yield helper.ensureExists(models.Testimonial, { id: f.postId });
97+
f.testimonial = yield helper.populateUsersForEntity(yield helper.ensureExists(models.Testimonial, { id: f.postId }));
9698
}
9799
return f;
98100
}

modules/lookups/routes.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,53 @@
77
/*
88
* Contains all routes.
99
*/
10-
const constants = require('../../constants');
1110

12-
const jwtAuth = constants.Passports.jwt;
1311

1412
module.exports = {
1513
'/branches': {
1614
get: {
17-
auth: jwtAuth,
1815
controller: 'LookupController',
1916
method: 'getBranches'
2017
}
2118
},
2219
'/ranks': {
2320
get: {
24-
auth: jwtAuth,
2521
controller: 'LookupController',
2622
method: 'getRanks'
2723
}
2824
},
2925
'/wars': {
3026
get: {
31-
auth: jwtAuth,
3227
controller: 'LookupController',
3328
method: 'getWars'
3429
}
3530
},
3631
'/countries': {
3732
get: {
38-
auth: jwtAuth,
3933
controller: 'LookupController',
4034
method: 'getCountries'
4135
}
4236
},
4337
'/cemeteries': {
4438
get: {
45-
auth: jwtAuth,
4639
controller: 'LookupController',
4740
method: 'getCemeteries'
4841
}
4942
},
5043
'/badgeTypes': {
5144
get: {
52-
auth: jwtAuth,
5345
controller: 'LookupController',
5446
method: 'getBadgeTypes'
5547
}
5648
},
5749
'/eventTypes': {
5850
get: {
59-
auth: jwtAuth,
6051
controller: 'LookupController',
6152
method: 'getEventTypes'
6253
}
6354
},
6455
'/campaigns': {
6556
get: {
66-
auth: jwtAuth,
6757
controller: 'LookupController',
6858
method: 'getCampaigns'
6959
}

modules/nextofkins/services/NextOfKinService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function* create(files, body) {
8686
const fileIds = [];
8787
for (let i = 0; i < files.length; i += 1) {
8888
const file = yield models.File.create({
89-
name: files[i].filename,
89+
name: files[i].originalname,
9090
fileURL: `${config.appURL}/upload/${files[i].filename}`,
9191
mimeType: files[i].mimetype
9292
}, { transaction: t });

modules/security/services/SecurityService.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ register.schema = {
6060
user: Joi.object().keys({
6161
username: Joi.string().required(),
6262
email: Joi.string().email().required(),
63-
firstName: Joi.string().required(),
64-
lastName: Joi.string().required(),
65-
mobile: Joi.string().required(),
66-
gender: Joi.string().required(),
67-
countryId: Joi.number().integer().min(1).required(),
63+
firstName: Joi.string().allow(['']),
64+
lastName: Joi.string().allow(['']),
65+
mobile: Joi.string().allow(['']),
66+
gender: Joi.string().allow(['']),
67+
countryId: Joi.number().integer().min(1),
6868
password: Joi.string().required()
6969
}).required()
7070
};

modules/stories/services/StoryService.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ function* search(query, user) {
5050
}
5151

5252
const q = buildDBFilter(query);
53+
if (query.review) {
54+
const nextOfKins = yield models.NextOfKin.find({
55+
where: {
56+
userId: user.id,
57+
status: models.modelConstants.Statuses.Approved
58+
}
59+
});
60+
q.where.veteranId = { $in: _.map(nextOfKins, item => item.veteranId) };
61+
}
5362
const docs = yield models.Story.findAndCountAll(q);
5463
return {
5564
items: yield helper.populateUsersForEntities(docs.rows),
@@ -64,6 +73,7 @@ search.schema = {
6473
veteranId: Joi.optionalId(),
6574
userId: Joi.optionalId(),
6675
status: Joi.string().valid(_.values(models.modelConstants.Statuses)),
76+
review: Joi.boolean(),
6777
limit: Joi.limit(),
6878
offset: Joi.offset(),
6979
sortColumn: Joi.string().valid('id', 'veteranId', 'title', 'text', 'status').default('id'),

modules/users/services/UserService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function* update(id, body, currentUser) {
105105

106106
// if current user is not admin, then he can only update himself, and role can not be changed
107107
if (currentUser.role !== models.modelConstants.UserRoles.Admin) {
108-
if (id !== currentUser.id) {
108+
if (id !== _.toNumber(currentUser.id)) {
109109
throw new ForbiddenError('You can not update other user.');
110110
}
111111
if (body.role && user.role !== body.role) {

modules/veterans/services/VeteransService.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ function buildDBFilter(filter) {
4949
}
5050
if (filter.birthDateStart) where.birthDate = { $gte: filter.birthDateStart };
5151
if (filter.birthDateEnd) {
52-
if (!filter.birthDate) filter.birthDate = {};
53-
filter.birthDate.$lte = filter.birthDateEnd;
52+
if (!where.birthDate) where.birthDate = {};
53+
where.birthDate.$lte = filter.birthDateEnd;
5454
}
5555
if (filter.deathDateStart) where.deathDate = { $gte: filter.deathDateStart };
5656
if (filter.deathDateEnd) {
57-
if (!filter.deathDate) filter.deathDate = {};
58-
filter.deathDate.$lte = filter.deathDateEnd;
57+
if (!where.deathDate) where.deathDate = {};
58+
where.deathDate.$lte = filter.deathDateEnd;
5959
}
6060
if (filter.squadronShip) where.squadronShip = { $like: `%${filter.squadronShip}%` };
6161
if (filter.branchIds) include[4].where = { id: { $in: parseIds(filter.branchIds) } };
@@ -76,13 +76,15 @@ function buildDBFilter(filter) {
7676
*/
7777
function* search(query) {
7878
const q = buildDBFilter(query);
79-
const items = yield models.Veteran.findAll(q);
79+
q.distinct = true;
80+
const docs = yield models.Veteran.findAndCountAll(q);
8081
// note that when child entities are included for filter, sequelize can not correctly calculate
8182
// the total count, so the total field is not provided
8283
return {
83-
items,
84+
items: docs.rows,
8485
offset: q.offset,
85-
limit: q.limit
86+
limit: q.limit,
87+
total: docs.count
8688
};
8789
}
8890

package-lock.json

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)