Skip to content

Commit

Permalink
Merge pull request #233 from adhocteam/js-360-filter-reports-to-approved
Browse files Browse the repository at this point in the history
Filter main report table to approved reports
  • Loading branch information
rahearn committed Mar 12, 2021
2 parents 1ed933c + 78ddff4 commit 9532b3c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
5 changes: 1 addition & 4 deletions src/services/activityReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export function activityReports(readRegions, {

return ActivityReport.findAndCountAll(
{
where: { regionId: regions },
where: { regionId: regions, status: REPORT_STATUSES.APPROVED },
attributes: [
'id',
'displayId',
Expand Down Expand Up @@ -386,9 +386,6 @@ export function activityReportAlerts(userId, {
{
status: { [Op.ne]: REPORT_STATUSES.APPROVED },
},
{
status: { [Op.ne]: REPORT_STATUSES.SUBMITTED },
},
],
},
{
Expand Down
107 changes: 68 additions & 39 deletions src/services/activityReports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jest.mock('./goals', () => ({
}));

const RECIPIENT_ID = 15;
const GRANTEE_ID = 16;

const mockUser = {
id: 1000,
Expand All @@ -26,6 +27,14 @@ const mockUser = {
hsesUserId: '1000',
};

const mockUserTwo = {
id: 1002,
homeRegionId: 1,
name: 'a user',
hsesUserId: 50,
hsesUsername: 'Rex',
};

const reportObject = {
activityRecipientType: 'grantee',
status: REPORT_STATUSES.DRAFT,
Expand Down Expand Up @@ -72,12 +81,12 @@ describe('Activity Reports DB service', () => {
await NextStep.destroy({ where: {} });
await ActivityRecipient.destroy({ where: {} });
await ActivityReport.destroy({ where: {} });
await User.destroy({ where: { id: mockUser.id } });
await User.destroy({ where: { id: [mockUser.id, mockUserTwo.id] } });
await NonGrantee.destroy({ where: { id: RECIPIENT_ID } });
await Grant.destroy({ where: { id: RECIPIENT_ID } });
await Grantee.destroy({ where: { id: RECIPIENT_ID } });
await Grant.destroy({ where: { id: [RECIPIENT_ID, GRANTEE_ID] } });
await Grantee.destroy({ where: { id: [RECIPIENT_ID, GRANTEE_ID] } });
await Region.destroy({ where: { id: 17 } });
db.sequelize.close();
await db.sequelize.close();
});

afterEach(() => {
Expand Down Expand Up @@ -292,31 +301,63 @@ describe('Activity Reports DB service', () => {
});

describe('activityReports retrieval and sorting', () => {
it('retrieves reports with default sort by updatedAt', async () => {
const report = await ActivityReport.create(reportObject);
let latestReport;
let firstGrant;

const { count, rows } = await activityReports([1], {});
expect(rows.length).toBe(10);
expect(count).toBeDefined();
expect(rows[0].id).toBe(report.id);
});
beforeAll(async () => {
const topicsOne = ['topic d', 'topic c'];
const topicsTwo = ['topic b', 'topic a'];
const firstGrantee = await Grantee.create({ id: GRANTEE_ID, name: 'aaaa' });
firstGrant = await Grant.create({ id: GRANTEE_ID, number: 'anumber', granteeId: firstGrantee.id });

it('retrieves reports sorted by author', async () => {
const mockUserTwo = {
id: 1002,
homeRegionId: 1,
name: 'a user',
hsesUsername: 'user',
hsesUserId: '1002',
};
await User.findOrCreate({
where: {
id: mockUserTwo.id,
},
defaults: mockUserTwo,
});
await ActivityReport.create({
...submittedReport,
status: REPORT_STATUSES.APPROVED,
userId: mockUserTwo.id,
topics: topicsOne,
});
await createOrUpdate({
...submittedReport,
status: REPORT_STATUSES.APPROVED,
collaborators: [{ id: mockUser.id }],
});
await ActivityReport.create({
...submittedReport,
status: REPORT_STATUSES.APPROVED,
regionId: 2,
});
const report = await ActivityReport.create({
...submittedReport,
activityRecipients: [{ grantId: firstGrant.id }],
status: REPORT_STATUSES.APPROVED,
topics: topicsTwo,
});
await ActivityRecipient.create({
activityReportId: report.id,
grantId: firstGrant.id,
});
latestReport = await ActivityReport.create({
...submittedReport,
status: REPORT_STATUSES.APPROVED,
updatedAt: '1900-01-01T12:00:00Z',
});
});

it('retrieves reports with default sort by updatedAt', async () => {
const { count, rows } = await activityReports([1], {});
expect(rows.length).toBe(6);
expect(count).toBeDefined();
expect(rows[0].id).toBe(latestReport.id);
});

it('retrieves reports sorted by author', async () => {
reportObject.userId = mockUserTwo.id;
await ActivityReport.create(reportObject);

const { rows } = await activityReports([1], {
sortBy: 'author', sortDir: 'asc', offset: 0, limit: 2,
Expand All @@ -331,7 +372,7 @@ describe('Activity Reports DB service', () => {
const { rows } = await activityReports([1], {
sortBy: 'collaborators', sortDir: 'asc', offset: 0, limit: 12,
});
expect(rows.length).toBe(12);
expect(rows.length).toBe(6);
expect(rows[0].collaborators[0].name).toBe('user');
});

Expand All @@ -342,31 +383,26 @@ describe('Activity Reports DB service', () => {
const { rows } = await activityReports([1, 2], {
sortBy: 'regionId', sortDir: 'desc', offset: 0, limit: 12,
});
expect(rows.length).toBe(12);
expect(rows.length).toBe(7);
expect(rows[0].regionId).toBe(2);
});

it('retrieves reports sorted by activity recipients', async () => {
reportObject.regionId = 2;
await ActivityReport.create(reportObject);

const { rows } = await activityReports([1, 2], {
sortBy: 'activityRecipients', sortDir: 'asc', offset: 0, limit: 12,
});
expect(rows.length).toBe(12);
expect(rows[0].activityRecipients[0].activityRecipientId).toBe(RECIPIENT_ID);
expect(rows.length).toBe(7);
expect(rows[0].activityRecipients[0].grantId).toBe(firstGrant.id);
});

it('retrieves reports sorted by sorted topics', async () => {
reportObject.topics = ['topic d', 'topic c'];
await ActivityReport.create(reportObject);
reportObject.topics = ['topic b', 'topic a'];
await ActivityReport.create(reportObject);

const { rows } = await activityReports([1, 2], {
sortBy: 'topics', sortDir: 'asc', offset: 0, limit: 12,
});
expect(rows.length).toBe(12);
expect(rows.length).toBe(7);
expect(rows[0].sortedTopics[0]).toBe('topic a');
expect(rows[0].sortedTopics[1]).toBe('topic b');
expect(rows[1].sortedTopics[0]).toBe('topic c');
Expand All @@ -376,13 +412,6 @@ describe('Activity Reports DB service', () => {
});

it('retrieves myalerts', async () => {
const mockUserTwo = {
id: 1002,
homeRegionId: 1,
name: 'a user',
hsesUserId: 50,
hsesUsername: 'Rex',
};
await User.findOrCreate({
where: {
id: mockUserTwo.id,
Expand All @@ -393,8 +422,8 @@ describe('Activity Reports DB service', () => {
await ActivityReport.create(reportObject);

const { count, rows } = await activityReportAlerts(mockUserTwo.id, {});
expect(count).toBe(7);
expect(rows.length).toBe(7);
expect(count).toBe(5);
expect(rows.length).toBe(5);
expect(rows[0].userId).toBe(mockUserTwo.id);
});
});
Expand Down

0 comments on commit 9532b3c

Please sign in to comment.