Skip to content

Commit 6f53d37

Browse files
authored
Merge pull request #2669 from Northeastern-Electric-Racing/#2636-get-all-useful-links
#2636 created getAllUsefulLinks endpoint
2 parents ec52fc8 + eca4678 commit 6f53d37

5 files changed

Lines changed: 83 additions & 2 deletions

File tree

src/backend/src/controllers/organizations.controller.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,15 @@ export default class OrganizationsController {
1616
next(error);
1717
}
1818
}
19+
20+
static async getAllUsefulLinks(req: Request, res: Response, next: NextFunction) {
21+
try {
22+
const organizationId = getOrganizationId(req.headers);
23+
24+
const links = await OrganizationsService.getAllUsefulLinks(organizationId);
25+
res.status(200).json(links);
26+
} catch (error: unknown) {
27+
next(error);
28+
}
29+
}
1930
}

src/backend/src/prisma/seed.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import UsersService from '../services/users.services';
3131
import { transformDate } from '../utils/datetime.utils';
3232
import { writeFileSync } from 'fs';
3333
import WorkPackageTemplatesService from '../services/work-package-template.services';
34+
import OrganizationsService from '../services/organizations.service';
3435

3536
const prisma = new PrismaClient();
3637

@@ -1941,6 +1942,19 @@ const performSeed: () => Promise<void> = async () => {
19411942
[schematicWpTemplate.workPackageTemplateId],
19421943
organizationId
19431944
);
1945+
1946+
await OrganizationsService.setUsefulLinks(batman, organizationId, [
1947+
{
1948+
linkId: '1',
1949+
linkTypeName: 'Confluence',
1950+
url: 'https://google.com'
1951+
},
1952+
{
1953+
linkId: '2',
1954+
linkTypeName: 'Bill of Materials',
1955+
url: 'https://apple.com'
1956+
}
1957+
]);
19441958
};
19451959

19461960
performSeed()

src/backend/src/routes/organizations.routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ import OrganizationsController from '../controllers/organizations.controller';
55
const organizationRouter = express.Router();
66

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

910
export default organizationRouter;

src/backend/src/services/organizations.service.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { User } from '@prisma/client';
22
import { LinkCreateArgs, isAdmin } from 'shared';
33
import prisma from '../prisma/prisma';
4-
import { AccessDeniedAdminOnlyException, HttpException } from '../utils/errors.utils';
4+
import { AccessDeniedAdminOnlyException, HttpException, NotFoundException } from '../utils/errors.utils';
55
import { userHasPermission } from '../utils/users.utils';
66
import { createUsefulLinks } from '../utils/organizations.utils';
77

@@ -53,4 +53,27 @@ export default class OrganizationsService {
5353

5454
return newLinks;
5555
}
56+
57+
/**
58+
Gets all the useful links for an organization
59+
@param organizationId the organization to get the links for
60+
@returns the useful links for the organization
61+
*/
62+
static async getAllUsefulLinks(organizationId: string) {
63+
const organization = await prisma.organization.findUnique({
64+
where: { organizationId },
65+
select: { usefulLinks: { select: { linkId: true } } }
66+
});
67+
68+
if (!organization) {
69+
throw new NotFoundException('Organization', organizationId);
70+
}
71+
72+
const links = await prisma.link.findMany({
73+
where: {
74+
linkId: { in: organization.usefulLinks.map((link) => link.linkId) }
75+
}
76+
});
77+
return links;
78+
}
5679
}

src/backend/tests/unmocked/organization.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LinkCreateArgs } from 'shared';
22
import OrganizationsService from '../../src/services/organizations.service';
3-
import { AccessDeniedAdminOnlyException, HttpException } from '../../src/utils/errors.utils';
3+
import { AccessDeniedAdminOnlyException, HttpException, NotFoundException } from '../../src/utils/errors.utils';
44
import { batmanAppAdmin, wonderwomanGuest } from '../test-data/users.test-data';
55
import { createTestLinkType, createTestOrganization, createTestUser, resetUsers } from '../test-utils';
66
import prisma from '../../src/prisma/prisma';
@@ -96,4 +96,36 @@ describe('Team Type Tests', () => {
9696
expect(updatedOrganization!.usefulLinks[1].url).toBe('link 4');
9797
});
9898
});
99+
100+
describe('Get all Useful Links', () => {
101+
it('Fails if a organization does not exist', async () => {
102+
await expect(async () => await OrganizationsService.getAllUsefulLinks('1')).rejects.toThrow(
103+
new NotFoundException('Organization', '1')
104+
);
105+
});
106+
107+
it('succeeds and gets all the links', async () => {
108+
const testLinks1: LinkCreateArgs[] = [
109+
{
110+
linkId: '1',
111+
linkTypeName: 'Link type 1',
112+
url: 'link 1'
113+
},
114+
{
115+
linkId: '2',
116+
linkTypeName: 'Link type 1',
117+
url: 'link 2'
118+
}
119+
];
120+
const testBatman = await createTestUser(batmanAppAdmin, orgId);
121+
await createTestLinkType(testBatman, orgId);
122+
await OrganizationsService.setUsefulLinks(testBatman, orgId, testLinks1);
123+
const links = await OrganizationsService.getAllUsefulLinks(orgId);
124+
125+
expect(links).not.toBeNull();
126+
expect(links.length).toBe(2);
127+
expect(links[0].url).toBe('link 1');
128+
expect(links[1].url).toBe('link 2');
129+
});
130+
});
99131
});

0 commit comments

Comments
 (0)