Skip to content

Commit

Permalink
Merge pull request #2834 from Northeastern-Electric-Racing/#2764-Crea…
Browse files Browse the repository at this point in the history
…te-SetLogo-Endpoint

#2764 Create set logo endpoint
  • Loading branch information
caiodasilva2005 authored Sep 17, 2024
2 parents 05f8177 + d347b04 commit b2bab20
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/backend/src/controllers/organizations.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextFunction, Request, Response } from 'express';
import OrganizationsService from '../services/organizations.services';
import { HttpException } from '../utils/errors.utils';

export default class OrganizationsController {
static async setUsefulLinks(req: Request, res: Response, next: NextFunction) {
Expand Down Expand Up @@ -51,4 +52,17 @@ export default class OrganizationsController {
next(error);
}
}

static async setLogoImage(req: Request, res: Response, next: NextFunction) {
try {
if (!req.file) {
throw new HttpException(400, 'Invalid or undefined image data');
}
const updatedOrg = await OrganizationsService.setLogoImage(req.file, req.currentUser, req.organization);

return res.status(200).json(updatedOrg);
} catch (error: unknown) {
return 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 @@ -18,4 +18,5 @@ organizationRouter.post(
);

organizationRouter.get('/images', OrganizationsController.getOrganizationImages);
organizationRouter.post('/logo/update', upload.single('logo'), OrganizationsController.setLogoImage);
export default organizationRouter;
29 changes: 29 additions & 0 deletions src/backend/src/services/organizations.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,33 @@ export default class OrganizationsService {
exploreAsGuestImage: organization.exploreAsGuestImageId
};
}

/**
* Sets the logo for an organization, User must be admin
* @param logoImage the image which will be uploaded and have its id stored in the org
* @param submitter the user submitting the logo
* @param organization the organization who's logo is being set
* @returns the updated organization
* @throws if the user is not an admin
*/
static async setLogoImage(
logoImage: Express.Multer.File,
submitter: User,
organization: Organization
): Promise<Organization> {
if (!(await userHasPermission(submitter.userId, organization.organizationId, isAdmin))) {
throw new AccessDeniedAdminOnlyException('update logo');
}

const logoImageData = await uploadFile(logoImage);

const updatedOrg = await prisma.organization.update({
where: { organizationId: organization.organizationId },
data: {
logoImageId: logoImageData.id
}
});

return updatedOrg;
}
}
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 @@ -190,4 +190,42 @@ describe('Team Type Tests', () => {
expect(images.exploreAsGuestImage).toBe('uploaded-image2.png');
});
});

describe('Set Logo', () => {
const file1 = { originalname: 'image1.png' } as Express.Multer.File;
const file2 = { originalname: 'image2.png' } as Express.Multer.File;
it('Fails if user is not an admin', async () => {
await expect(
OrganizationsService.setLogoImage(file1, await createTestUser(wonderwomanGuest, orgId), organization)
).rejects.toThrow(new AccessDeniedAdminOnlyException('update logo'));
});

it('Succeeds and updates the logo', async () => {
const testBatman = await createTestUser(batmanAppAdmin, orgId);
(uploadFile as Mock).mockImplementation((file) => {
return Promise.resolve({ id: `uploaded-${file.originalname}` });
});

await OrganizationsService.setLogoImage(file1, testBatman, organization);

const oldOrganization = await prisma.organization.findUnique({
where: {
organizationId: orgId
}
});

expect(oldOrganization).not.toBeNull();
expect(oldOrganization?.logoImageId).toBe('uploaded-image1.png');

await OrganizationsService.setLogoImage(file2, testBatman, organization);

const updatedOrganization = await prisma.organization.findUnique({
where: {
organizationId: orgId
}
});

expect(updatedOrganization?.logoImageId).toBe('uploaded-image2.png');
});
});
});

0 comments on commit b2bab20

Please sign in to comment.