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 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
11 changes: 11 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,15 @@ export default class OrganizationsController {
next(error);
}
}

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

const links = await OrganizationsService.getAllUsefulLinks(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;
25 changes: 24 additions & 1 deletion src/backend/src/services/organizations.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { User } from '@prisma/client';
import { LinkCreateArgs, isAdmin } from 'shared';
import prisma from '../prisma/prisma';
import { AccessDeniedAdminOnlyException, HttpException } from '../utils/errors.utils';
import { AccessDeniedAdminOnlyException, HttpException, NotFoundException } from '../utils/errors.utils';
import { userHasPermission } from '../utils/users.utils';
import { createUsefulLinks } from '../utils/organizations.utils';

Expand Down Expand Up @@ -53,4 +53,27 @@ export default class OrganizationsService {

return newLinks;
}

/**
Gets all the useful links for an organization
@param organizationId the organization to get the links for
@returns the useful links for the organization
*/
static async getAllUsefulLinks(organizationId: string) {
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 NotFoundException('Organization', organizationId);
}

const links = await prisma.link.findMany({
where: {
linkId: { in: organization.usefulLinks.map((link) => link.linkId) }
}
});
return links;
}
}
34 changes: 33 additions & 1 deletion src/backend/tests/unmocked/organization.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LinkCreateArgs } from 'shared';
import OrganizationsService from '../../src/services/organizations.service';
import { AccessDeniedAdminOnlyException, HttpException } from '../../src/utils/errors.utils';
import { AccessDeniedAdminOnlyException, HttpException, NotFoundException } from '../../src/utils/errors.utils';
import { batmanAppAdmin, wonderwomanGuest } from '../test-data/users.test-data';
import { createTestLinkType, createTestOrganization, createTestUser, resetUsers } from '../test-utils';
import prisma from '../../src/prisma/prisma';
Expand Down Expand Up @@ -96,4 +96,36 @@ describe('Team Type Tests', () => {
expect(updatedOrganization!.usefulLinks[1].url).toBe('link 4');
});
});

describe('Get all Useful Links', () => {
it('Fails if a organization does not exist', async () => {
await expect(async () => await OrganizationsService.getAllUsefulLinks('1')).rejects.toThrow(
new NotFoundException('Organization', '1')
);
});

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(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