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

#2636 created getAllUsefulLinks endpoint #2669

Merged
merged 8 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions src/backend/src/controllers/organizations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ export default class OrganizationsController {
next(error);
}
}

static async getAllUsefulLinks(req: Request, res: Response, next: NextFunction) {
try {
const submitter = await getCurrentUser(res);
const organizationId = getOrganizationId(req.headers);

const links = await OrganizationsService.getAllUsefulLinks(submitter, organizationId);
res.status(200).json(links);
} catch (error: unknown) {
next(error);
}
}
}
14 changes: 14 additions & 0 deletions src/backend/src/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import UsersService from '../services/users.services';
import { transformDate } from '../utils/datetime.utils';
import { writeFileSync } from 'fs';
import WorkPackageTemplatesService from '../services/work-package-template.services';
import OrganizationsService from '../services/organizations.service';

const prisma = new PrismaClient();

Expand Down Expand Up @@ -1941,6 +1942,19 @@ const performSeed: () => Promise<void> = async () => {
[schematicWpTemplate.workPackageTemplateId],
organizationId
);

await OrganizationsService.setUsefulLinks(batman, organizationId, [
{
linkId: '1',
linkTypeName: 'Confluence',
url: 'https://google.com'
},
{
linkId: '2',
linkTypeName: 'Bill of Materials',
url: 'https://apple.com'
}
]);
};

performSeed()
Expand Down
1 change: 1 addition & 0 deletions src/backend/src/routes/organizations.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import OrganizationsController from '../controllers/organizations.controller';
const organizationRouter = express.Router();

organizationRouter.post('/useful-links/set', ...linkValidators, validateInputs, OrganizationsController.setUsefulLinks);
organizationRouter.get('/useful-links', OrganizationsController.getAllUsefulLinks);

export default organizationRouter;
21 changes: 21 additions & 0 deletions src/backend/src/services/organizations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,25 @@ export default class OrganizationsService {

return newLinks;
}

static async getAllUsefulLinks(submitter: User, organizationId: string) {
Aaryan1203 marked this conversation as resolved.
Show resolved Hide resolved
if (!(await userHasPermission(submitter.userId, organizationId, isAdmin)))
Aaryan1203 marked this conversation as resolved.
Show resolved Hide resolved
throw new AccessDeniedAdminOnlyException('get useful links');

const organization = await prisma.organization.findUnique({
where: { organizationId },
select: { usefulLinks: { select: { linkId: true } } }
});

if (!organization) {
Aaryan1203 marked this conversation as resolved.
Show resolved Hide resolved
throw new HttpException(400, `Organization with id ${organizationId} doesn't exist`);
}

const links = await prisma.link.findMany({
where: {
linkId: { in: organization.usefulLinks.map((link) => link.linkId) }
}
});
return links;
}
}
38 changes: 38 additions & 0 deletions src/backend/tests/unmocked/organization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,42 @@ describe('Team Type Tests', () => {
expect(updatedOrganization!.usefulLinks[1].url).toBe('link 4');
});
});

describe('Get all Useful Links', () => {
it('Fails if user is not an admin', async () => {
await expect(
async () => await OrganizationsService.getAllUsefulLinks(await createTestUser(wonderwomanGuest, orgId), orgId)
).rejects.toThrow(new AccessDeniedAdminOnlyException('get useful links'));
});

it('Fails if a organization does not exist', async () => {
await expect(
async () => await OrganizationsService.getAllUsefulLinks(await createTestUser(batmanAppAdmin, orgId), '1')
).rejects.toThrow(new HttpException(400, `Organization with id: 1 not found!`));
});

it('succeeds and gets all the links', async () => {
const testLinks1: LinkCreateArgs[] = [
{
linkId: '1',
linkTypeName: 'Link type 1',
url: 'link 1'
},
{
linkId: '2',
linkTypeName: 'Link type 1',
url: 'link 2'
}
];
const testBatman = await createTestUser(batmanAppAdmin, orgId);
await createTestLinkType(testBatman, orgId);
await OrganizationsService.setUsefulLinks(testBatman, orgId, testLinks1);
const links = await OrganizationsService.getAllUsefulLinks(testBatman, orgId);

expect(links).not.toBeNull();
expect(links.length).toBe(2);
expect(links[0].url).toBe('link 1');
expect(links[1].url).toBe('link 2');
});
});
});
Loading