Skip to content

Commit

Permalink
Create editFlyer mutation (#105)
Browse files Browse the repository at this point in the history
* Add `editFlyer`

* Address PR comments
  • Loading branch information
vinnie4k committed Sep 26, 2023
1 parent ed241dc commit ee2aed9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/middlewares/FlyerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import utils from '../utils';

const FlyerUploadErrorInterceptor: MiddlewareFn<Context> = async ({ args, context }, next) => {
try {
// Upload image to our upload service
context.imageURL = await utils.uploadImage(args.imageB64);
// Upload image to our upload service (if not null)
if (args.imageB64) {
context.imageURL = await utils.uploadImage(args.imageB64);
return await next();
}

// If null, move on
return await next();
} catch (err) {
throw new Error('An error occured while uploading the flyer image.');
Expand Down
47 changes: 47 additions & 0 deletions src/repos/FlyerRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,56 @@ const deleteFlyer = async (id: string): Promise<Flyer> => {
return flyer;
};

/**
* Edit an existing flyer.
*
* @param {string} id the ID of the flyer to edit
* @param {string} categorySlug the slug for this flyer's category
* @param {string} endDate the end date for this flyer's event in UTC ISO8601 format
* @param {string} flyerURL the URL for this flyer when tapped
* @param {string} imageURL the URL representing this flyer's image
* @param {string} location the location for this flyer's event
* @param {string} startDate the start date for this flyer's event in UTC ISO8601 format
* @param {string} title the title for this flyer
* @returns the edited Flyer
*/
const editFlyer = async (
id: string,
categorySlug: string = null,
endDate: string = null,
flyerURL: string = null,
imageURL: string = null,
location: string = null,
startDate: string = null,
title: string = null,
): Promise<Flyer> => {
// Fetch flyer given flyer ID
const flyer = await FlyerModel.findById(new ObjectId(id));

if (flyer) {
// Remove existing image from our servers (if not null)
if (imageURL) await utils.removeImage(flyer.imageURL);

// Update flyer fields (if not nul)
if (categorySlug) flyer.categorySlug = categorySlug;
if (endDate) flyer.endDate = new Date(endDate);
if (flyerURL) flyer.flyerURL = flyerURL;
if (imageURL) flyer.imageURL = imageURL;
if (location) flyer.location = location;
if (startDate) flyer.startDate = new Date(startDate);
if (title) flyer.title = title;

return flyer.save();
}

// Flyer not found
return flyer;
};

export default {
createFlyer,
deleteFlyer,
editFlyer,
getAllFlyers,
getFlyerByID,
getFlyersAfterDate,
Expand Down
29 changes: 29 additions & 0 deletions src/resolvers/FlyerResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ class FlyerResolver {
async deleteFlyer(@Arg('id') id: string) {
return FlyerRepo.deleteFlyer(id);
}

@Mutation((_returns) => Flyer, {
description: `Edit <Flyer> with ID <id> via given <categorySlug>, <endDate>, <flyerURL>, <imageB64>, <location>, <startDate>, and <title>.
<startDate> and <endDate> must be in UTC ISO8601 format (e.g. YYYY-mm-ddTHH:MM:ssZ).
<imageB64> must be a Base64 encrypted string without 'data:image/png;base64,' prepended`,
})
@UseMiddleware(FlyerMiddleware.FlyerUploadErrorInterceptor)
async editFlyer(
@Arg('id') id: string,
@Arg('categorySlug', { nullable: true }) categorySlug: string,
@Arg('endDate', { nullable: true }) endDate: string,
@Arg('flyerURL', { nullable: true }) flyerURL: string,
@Arg('imageB64', { nullable: true }) imageB64: string,
@Arg('location', { nullable: true }) location: string,
@Arg('startDate', { nullable: true }) startDate: string,
@Arg('title', { nullable: true }) title: string,
@Ctx() ctx: Context,
) {
return FlyerRepo.editFlyer(
id,
categorySlug,
endDate,
flyerURL,
ctx.imageURL,
location,
startDate,
title,
);
}
}

export default FlyerResolver;
24 changes: 23 additions & 1 deletion src/tests/flyer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,26 @@ describe('deleteFlyer tests', () => {
const deleteFlyerResponse = await FlyerRepo.deleteFlyer('64811792f910705ca1a981f8');
expect(deleteFlyerResponse).toBeNull();
});
});
});

describe('editFlyer tests', () => {
test('flyer with ID exists with changes', async () => {
const flyers = await FlyerFactory.create(2);
await FlyerModel.insertMany(flyers);

const fetchedFlyers = await FlyerRepo.getAllFlyers();
const firstFlyer = fetchedFlyers[0];

const randomSlug = Math.random().toString();
const editFlyerResponse = await FlyerRepo.editFlyer(firstFlyer.id, randomSlug);
expect(editFlyerResponse.categorySlug).toStrictEqual(randomSlug);
});

test('flyer with ID does not exist', async () => {
const flyers = await FlyerFactory.create(2);
await FlyerModel.insertMany(flyers);

const editFlyerResponse = await FlyerRepo.editFlyer('64811792f910705ca1a981f8');
expect(editFlyerResponse).toBeNull();
});
});

0 comments on commit ee2aed9

Please sign in to comment.