Skip to content

Commit c2d1e76

Browse files
author
Vikas Agarwal
committed
Added filtering on productKey for product templates
1 parent 9cb3bc3 commit c2d1e76

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

src/routes/productTemplates/list.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ const permissions = tcMiddleware.permissions;
99

1010
module.exports = [
1111
permissions('productTemplate.view'),
12-
(req, res, next) => models.ProductTemplate.findAll({
13-
where: {
14-
deletedAt: { $eq: null },
15-
},
16-
attributes: { exclude: ['deletedAt', 'deletedBy'] },
17-
raw: true,
18-
})
12+
(req, res, next) => {
13+
const filters = util.parseQueryFilter(req.query.filter);
14+
if (!util.isValidFilter(filters, ['productKey'])) {
15+
return util.handleError('Invalid filters', null, req, next);
16+
}
17+
const where = { deletedAt: { $eq: null } };
18+
if (filters.productKey) {
19+
where.productKey = { $eq: filters.productKey };
20+
}
21+
return models.ProductTemplate.findAll({
22+
where,
23+
attributes: { exclude: ['deletedAt', 'deletedBy'] },
24+
raw: true,
25+
})
1926
.then((productTemplates) => {
2027
res.json(util.wrapResponse(req.id, productTemplates));
2128
})
22-
.catch(next),
29+
.catch(next);
30+
},
2331
];

src/routes/productTemplates/list.spec.js

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
/**
22
* Tests for list.js
33
*/
4-
import chai from 'chai';
4+
// import chai from 'chai';
55
import request from 'supertest';
66

77
import models from '../../models';
88
import server from '../../app';
99
import testUtil from '../../tests/util';
1010

11-
const should = chai.should();
11+
// const should = chai.should();
12+
13+
const validateProductTemplates = (count, resJson, expectedTemplates) => {
14+
resJson.should.have.length(count);
15+
resJson.forEach((pt, idx) => {
16+
pt.should.have.all.keys('id', 'name', 'productKey', 'icon', 'brief', 'details', 'aliases',
17+
'template', 'createdBy', 'createdAt', 'updatedBy', 'updatedAt');
18+
pt.should.not.have.all.keys('deletedAt', 'deletedBy');
19+
pt.name.should.be.eql(expectedTemplates[idx].name);
20+
pt.productKey.should.be.eql(expectedTemplates[idx].productKey);
21+
pt.icon.should.be.eql(expectedTemplates[idx].icon);
22+
pt.brief.should.be.eql(expectedTemplates[idx].brief);
23+
pt.details.should.be.eql(expectedTemplates[idx].details);
24+
pt.aliases.should.be.eql(expectedTemplates[idx].aliases);
25+
pt.template.should.be.eql(expectedTemplates[idx].template);
26+
pt.createdBy.should.be.eql(expectedTemplates[idx].createdBy);
27+
pt.updatedBy.should.be.eql(expectedTemplates[idx].updatedBy);
28+
});
29+
};
1230

1331
describe('LIST product templates', () => {
1432
const templates = [
1533
{
1634
name: 'name 1',
17-
productKey: 'productKey 1',
35+
productKey: 'productKey-1',
1836
icon: 'http://example.com/icon1.ico',
1937
brief: 'brief 1',
2038
details: 'details 1',
@@ -46,7 +64,7 @@ describe('LIST product templates', () => {
4664
},
4765
{
4866
name: 'template 2',
49-
productKey: 'productKey 2',
67+
productKey: 'productKey-2',
5068
icon: 'http://example.com/icon2.ico',
5169
brief: 'brief 2',
5270
details: 'details 2',
@@ -83,26 +101,9 @@ describe('LIST product templates', () => {
83101
})
84102
.expect(200)
85103
.end((err, res) => {
86-
const template = templates[0];
87-
88104
const resJson = res.body.result.content;
89-
resJson.should.have.length(2);
105+
validateProductTemplates(2, resJson, templates);
90106
resJson[0].id.should.be.eql(templateId);
91-
resJson[0].name.should.be.eql(template.name);
92-
resJson[0].productKey.should.be.eql(template.productKey);
93-
resJson[0].icon.should.be.eql(template.icon);
94-
resJson[0].brief.should.be.eql(template.brief);
95-
resJson[0].details.should.be.eql(template.details);
96-
resJson[0].aliases.should.be.eql(template.aliases);
97-
resJson[0].template.should.be.eql(template.template);
98-
99-
resJson[0].createdBy.should.be.eql(template.createdBy);
100-
should.exist(resJson[0].createdAt);
101-
resJson[0].updatedBy.should.be.eql(template.updatedBy);
102-
should.exist(resJson[0].updatedAt);
103-
should.not.exist(resJson[0].deletedBy);
104-
should.not.exist(resJson[0].deletedAt);
105-
106107
done();
107108
});
108109
});
@@ -114,7 +115,12 @@ describe('LIST product templates', () => {
114115
Authorization: `Bearer ${testUtil.jwts.connectAdmin}`,
115116
})
116117
.expect(200)
117-
.end(done);
118+
.end((err, res) => {
119+
const resJson = res.body.result.content;
120+
validateProductTemplates(2, resJson, templates);
121+
resJson[0].id.should.be.eql(templateId);
122+
done();
123+
});
118124
});
119125

120126
it('should return 200 for connect manager', (done) => {
@@ -124,7 +130,12 @@ describe('LIST product templates', () => {
124130
Authorization: `Bearer ${testUtil.jwts.manager}`,
125131
})
126132
.expect(200)
127-
.end(done);
133+
.end((err, res) => {
134+
const resJson = res.body.result.content;
135+
validateProductTemplates(2, resJson, templates);
136+
resJson[0].id.should.be.eql(templateId);
137+
done();
138+
});
128139
});
129140

130141
it('should return 200 for member', (done) => {
@@ -133,7 +144,12 @@ describe('LIST product templates', () => {
133144
.set({
134145
Authorization: `Bearer ${testUtil.jwts.member}`,
135146
})
136-
.expect(200, done);
147+
.end((err, res) => {
148+
const resJson = res.body.result.content;
149+
validateProductTemplates(2, resJson, templates);
150+
resJson[0].id.should.be.eql(templateId);
151+
done();
152+
});
137153
});
138154

139155
it('should return 200 for copilot', (done) => {
@@ -142,7 +158,26 @@ describe('LIST product templates', () => {
142158
.set({
143159
Authorization: `Bearer ${testUtil.jwts.copilot}`,
144160
})
145-
.expect(200, done);
161+
.end((err, res) => {
162+
const resJson = res.body.result.content;
163+
validateProductTemplates(2, resJson, templates);
164+
resJson[0].id.should.be.eql(templateId);
165+
done();
166+
});
167+
});
168+
169+
it('should return filtered templates', (done) => {
170+
request(server)
171+
.get('/v4/productTemplates?filter=productKey%3DproductKey-2')
172+
.set({
173+
Authorization: `Bearer ${testUtil.jwts.manager}`,
174+
})
175+
.expect(200)
176+
.end((err, res) => {
177+
const resJson = res.body.result.content;
178+
validateProductTemplates(1, resJson, [templates[1]]);
179+
done();
180+
});
146181
});
147182
});
148183
});

0 commit comments

Comments
 (0)