Skip to content

Commit

Permalink
chore: merges branch 'devel' of github.com:fga-eps-mds/2021.1-Oraculo…
Browse files Browse the repository at this point in the history
…-Registros into devel
  • Loading branch information
lucasvmx committed Nov 12, 2021
2 parents 90734dc + c3acc0b commit 14e7eb4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
34 changes: 24 additions & 10 deletions src/Controller/RecordController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const History = require("../Model/History");
const { User } = require("../Model/User");
const { Situation } = require("../Model/Situation");
const { Tag } = require("../Model/Tag");

const {
ERR_RECORD_NOT_FOUND,
ERR_NO_ERROR,
Expand Down Expand Up @@ -79,6 +78,7 @@ async function createRecord(req, res) {
receipt_form,
contact_info,
created_by,
tags
} = req.body);

try {
Expand Down Expand Up @@ -118,9 +118,8 @@ async function createRecord(req, res) {
record_id: createdRecord.id,
};

const tag = await Tag.findOne({ where: { name: "Tramitar" } });
await createdRecord.setDepartments([department]);
await createdRecord.setTags([tag]);
await createdRecord.setTags(tags);

await History.create(history);

Expand Down Expand Up @@ -150,30 +149,43 @@ async function getRecordsByPage(req, res) {
'reason'
]

const { exact = {}, history, ..._where } = where || {};
const tagFields = [
'name',
'color'
]

const { history, tag, ..._where } = where || {};

let filters = {};
const filters = {};
const historyFilters = [];
const tagFilters = [];

Object.entries(_where).forEach(([key, value]) => {
filters[key] = {
[Op.like]: "%" + value + "%"
[Op.iLike]: `%${value}%`
}
});

if(history) {
historyFields.forEach((item) => {
historyFilters.push({[item]: {
[Op.like]: "%" + history + "%"
[Op.iLike]: `%${history}%`
}})
});
}

filters = { ...exact, ...filters };
if(tag) {
tagFields.forEach((item) => {
tagFilters.push({[item]: {
[Op.iLike]: `%${tag}%`
}})
});
}

const { rows, count } = await Record.findAndCountAll({
include: [{ model: History, as: 'histories', ...(history && { where: { [Op.or]: historyFilters }})},
{ model: Department, as: 'departments', ...(department_id && { where: { id: department_id}})}],
{ model: Tag, as: 'tags', ...(tag && { where: { [Op.or]: tagFilters }})},
{ model: Department, as: 'departments', ...(department_id && { where: { id: department_id}})}],
where: filters,
limit: itemsPerPage,
order: [['register_number', 'ASC']],
Expand Down Expand Up @@ -429,6 +441,7 @@ async function editRecord(req, res) {
sei_number,
receipt_form,
contact_info,
tags
} = req.body);

try {
Expand All @@ -452,6 +465,7 @@ async function editRecord(req, res) {
await record.save();

const editedRecord = await Record.findByPk(recordID);
await editedRecord.setTags(tags);

return res.status(200).json(editedRecord);
} catch (err) {
Expand Down
11 changes: 8 additions & 3 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const validRecord1 = {
receipt_form: "form",
contact_info: "[email protected]",
created_by: "[email protected]",
tags: []
};

const validRecord2 = {
Expand All @@ -29,6 +30,7 @@ const validRecord2 = {
contact_info: "[email protected]",
situation: 2,
created_by: "[email protected]",
tags: []
};

const invalidRecord1 = {
Expand Down Expand Up @@ -102,7 +104,10 @@ describe("Main test", () => {
const res1 = await request(app).post("/records").send(validRecord1);
expect(res1.statusCode).toEqual(200);

const res = await request(app).get("/records/1/tags");
const res2 = await request(app).post(`/records/${res1.body.id}/add-tag`).send({ tag_id: 1 });
expect(res2.statusCode).toEqual(200);

const res = await request(app).get(`/records/${res1.body.id}/tags`);
expect(res.statusCode).toEqual(200);
});

Expand Down Expand Up @@ -322,7 +327,7 @@ describe("Main test", () => {
expect(res.body.message).toBeDefined();
});

it("GET /records/1/tags - should list tags of a record (empty tags)", async () => {
it("GET /records/:id/tags - should list tags of a record (empty tags)", async () => {
const res = await request(app).get("/records/1/tags");
expect(res.statusCode).toEqual(200);
});
Expand Down Expand Up @@ -354,7 +359,7 @@ describe("Main test", () => {
it("POST /records/:id/edit - should edit a record", async () => {
const res = await request(app)
.post("/records/1/edit")
.send({ city: "Goiania" });
.send({ city: "Goiania", tags: [] });
expect(res.statusCode).toEqual(200);
expect(res.body).toBeDefined();
});
Expand Down

0 comments on commit 14e7eb4

Please sign in to comment.