Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IPCT1-740] - list reports #317

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/api/src/controllers/v2/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ class UserController {
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

public getReport = (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 400, false, '', {
error: {
name: 'USER_NOT_FOUND',
message: 'User not identified!',
},
});
return;
}

this.userService
.getReport(req.user.address)
.then((r) => standardResponse(res, 201, true, r))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

public getLogs = (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 400, false, '', {
Expand Down
21 changes: 19 additions & 2 deletions packages/api/src/routes/v2/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,23 @@ export default (app: Router): void => {
userController.report
);

/**
* @swagger
*
* /users/report:
* get:
* tags:
* - "users"
* summary: "List anonymous report"
* responses:
* "200":
* description: "Success"
* security:
* - api_auth:
* - "write:modify":
*/
route.get('/report', authenticateToken, userController.getReport);

/**
* @swagger
*
Expand Down Expand Up @@ -348,7 +365,7 @@ export default (app: Router): void => {
* - api_auth:
* - "write:modify":
*/
route.get(
route.get(
'/notifications/unread',
authenticateToken,
userController.getUnreadNotifications
Expand Down Expand Up @@ -464,5 +481,5 @@ export default (app: Router): void => {
* - api_auth:
* - "write:modify":
*/
route.get('/:address', authenticateToken, userController.findBy);
route.get('/:address', authenticateToken, userController.findBy);
};
21 changes: 21 additions & 0 deletions packages/core/src/services/app/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ export default class UserService {
return true;
}

public async getReport(user: string) {
const communities = await models.community.findAll({
attributes: ['id'],
where: {
ambassadorAddress: user,
}
});

if (!communities || communities.length === 0) {
throw new BaseError('COMMUNITY_NOT_FOUND', 'no community found for this ambassador');
}

return models.anonymousReport.findAll({
where: {
communityId: {
[Op.in]: communities.map(c => c.id)
}
}
});
}

public async getPresignedUrlMedia(mime: string): Promise<{
uploadURL: string;
filename: string;
Expand Down
94 changes: 94 additions & 0 deletions packages/core/tests/integration/v2/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { expect } from 'chai';
import { Sequelize } from 'sequelize';

import { AppUser } from '../../../src/interfaces/app/appUser';
import { CommunityAttributes } from '../../../src/interfaces/ubi/community';
import UserService from '../../../src/services/app/user/index';
import { sequelizeSetup, truncate } from '../../config/sequelizeSetup';
import CommunityFactory from '../../factories/community';
import UserFactory from '../../factories/user';

describe('user service v2', () => {
let sequelize: Sequelize;
let users: AppUser[];
let communities: CommunityAttributes[];

const userService = new UserService();

before(async () => {
sequelize = sequelizeSetup();
await sequelize.sync();
users = await UserFactory({ n: 5 });

communities = await CommunityFactory([
{
requestByAddress: users[0].address,
started: new Date(),
status: 'valid',
visibility: 'public',
contract: {
baseInterval: 60 * 60 * 24,
claimAmount: '1000000000000000000',
communityId: 0,
incrementInterval: 5 * 60,
maxClaim: '450000000000000000000',
},
hasAddress: true,
ambassadorAddress: users[1].address,
},
{
requestByAddress: users[2].address,
started: new Date(),
status: 'valid',
visibility: 'public',
contract: {
baseInterval: 60 * 60 * 24,
claimAmount: '1000000000000000000',
communityId: 0,
incrementInterval: 5 * 60,
maxClaim: '450000000000000000000',
},
hasAddress: true,
ambassadorAddress: users[3].address,
},
]);
});

describe('report', () => {
after(async () => {
await truncate(sequelize, 'Community');
await truncate(sequelize, 'AppAnonymousReportModel');
});

describe('create', () => {
it('create successfully', async () => {
const firstReport = await userService.report(
'report',
communities[0].id,
'general'
);
const secondReport = await userService.report(
'report2',
communities[1].id,
'general'
);

expect(firstReport).to.be.true;
expect(secondReport).to.be.true;
});
});

describe('list', () => {
it('list by ambassador address', async () => {
const reports = await userService.getReport(users[1].address);

expect(reports.length).to.be.eq(1);
expect(reports[0]).to.include({
communityId: communities[0].id,
message: 'report',
category: 'general',
});
});
});
});
});