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

#3169 getparttags endpoint #3207

Open
wants to merge 4 commits into
base: feature/cad-project-file-review
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/backend/src/controllers/organizations.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,13 @@ export default class OrganizationsController {
next(error);
}
}

static async getAllPartTags(req: Request, res: Response, next: NextFunction) {
try {
const tags = await OrganizationsService.getAllPartTags(req.organization.organizationId);
res.status(200).json(tags);
} catch (error: unknown) {
next(error);
}
}
}
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 @@ -62,4 +62,5 @@ organizationRouter.post(
nonEmptyString(body('workspaceId')),
OrganizationsController.setSlackWorkspaceId
);
organizationRouter.get('/getAllPartTags', OrganizationsController.getAllPartTags);
export default organizationRouter;
27 changes: 27 additions & 0 deletions src/backend/src/services/organizations.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,31 @@ export default class OrganizationsService {

return updatedOrg;
}

/**
* Uses the given organizationID to and returns an array of part tags
* @param organizationId the organization to get the parts for
* @returns an array of part tags
*/
static async getAllPartTags(organizationId: string) {
const organization = await prisma.organization.findUnique({
where: { organizationId }
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the organization is pulled from the request and not an input from the user, you don't need to check for a notFoundException here. If the organization id for whatever reason did not exist here, it would be a problem on our end not the requester's end. Instead you can just get rid of this check


if (!organization) {
throw new NotFoundException('Organization', organizationId);
}

return prisma.partTag.findMany({
where: { organizationId },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only return partTags where dateDeleted is null

select: {
partTagId: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to include partTagId: true here

parts: {
select: {
partId: true
}
}
}
});
}
}
52 changes: 52 additions & 0 deletions src/backend/tests/unmocked/part-review.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { NotFoundException } from '../../src/utils/errors.utils';
import OrganizationsService from '../../src/services/organizations.services';
import { createTestOrganization, resetUsers } from '../test-utils';
import prisma from '../../src/prisma/prisma';
import { Organization } from '@prisma/client';

describe('Get All Part Tags', () => {
let organizationID: string;
let organization: Organization;

beforeEach(async () => {
organization = await createTestOrganization();
organizationID = organization.organizationId;
await prisma.partTag.deleteMany();
});

afterEach(async () => {
await resetUsers();
});

it('Fails if organization does not exist', async () => {
await expect(OrganizationsService.getAllPartTags('no-id')).rejects.toThrow(
new NotFoundException('Organization', 'no-id')
);
});

it('Succeeds and returns empty array', async () => {
const partTags = await OrganizationsService.getAllPartTags(organizationID);
expect(partTags).toBeInstanceOf(Array);
expect(partTags.length).toEqual(0);
});

it('Succeeds and returns part tags', async () => {
await prisma.partTag.createMany({
data: [
{
partTagId: '123',
name: 'Screw',
colorHexCode: '#191010',
dateCreated: new Date(),
organizationId: organizationID
},
{ partTagId: '456', name: 'Bolt', colorHexCode: '#093121', dateCreated: new Date(), organizationId: organizationID }
]
});

const partTags = await OrganizationsService.getAllPartTags(organizationID);
expect(partTags.length).toEqual(2);
expect(partTags.some((tag) => tag.partTagId === '123')).toBeTruthy();
expect(partTags.some((tag) => tag.partTagId === '456')).toBeTruthy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a partTag to a different organization, and a partTag that is deleted, and check that these are not in the result

});
});
12 changes: 12 additions & 0 deletions src/shared/src/types/parts-review.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Organization } from './user-types';

export interface PartTagReviewType {
partTagId: string;
name: string;
colorHexCode: string;
dateCreated: Date;
dateDeleted?: Date;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't include dateDeleted in the shared type. If it was deleted, people should not be seeing it on the frontend

parts: String[];
organization?: Organization;
organizationId: string;
}