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

#2764 Create set logo endpoint #2834

Merged
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
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) => {
chpy04 marked this conversation as resolved.
Show resolved Hide resolved
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');
});
});
});
Loading