Skip to content

Commit

Permalink
getFlyersByCategorySlug Query (#100)
Browse files Browse the repository at this point in the history
* Fixed notification issue

* Added flyer notification

* Added flyer notification

* Updated Organization and Flyer models

* Updated test cases

* Added getFlyersByCategorySlug query

* Update query description

* Inline notification calls

* Update flyer.test.ts

* Update flyer.test.ts
  • Loading branch information
vinnie4k authored Sep 12, 2023
1 parent 9ce89dd commit d49b504
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
26 changes: 15 additions & 11 deletions src/repos/FlyerRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ const getFlyersBeforeDate = (before: string, limit = DEFAULT_LIMIT): Promise<Fly
);
};

const getFlyersByCategorySlug = async (
categorySlug: string,
limit = DEFAULT_LIMIT,
offset = DEFAULT_OFFSET,
): Promise<Flyer[]> => {
return FlyerModel.find({ categorySlug })
.sort({ startDate: 'desc' })
.skip(offset)
.limit(limit)
.then((flyers) => {
return flyers.filter((flyer) => flyer !== null && !isFlyerFiltered(flyer));
});
};

const getFlyerByID = async (id: string): Promise<Flyer> => {
return FlyerModel.findById(new ObjectId(id)).then((flyer) => {
if (!isFlyerFiltered(flyer)) {
Expand Down Expand Up @@ -224,22 +238,12 @@ const incrementTimesClicked = async (id: string): Promise<Flyer> => {
return flyer;
};

/**
* Checks if an Flyer's title contains profanity.
* @function
* @param {string} title - Flyer title.
*/
const checkProfanity = async (title: string): Promise<boolean> => {
const filter = new Filter();
return filter.isProfane(title);
};

export default {
checkProfanity,
getAllFlyers,
getFlyerByID,
getFlyersAfterDate,
getFlyersBeforeDate,
getFlyersByCategorySlug,
getFlyersByIDs,
getFlyersByOrganizationID,
getFlyersByOrganizationIDs,
Expand Down
19 changes: 12 additions & 7 deletions src/resolvers/FlyerResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ class FlyerResolver {
return FlyerRepo.searchFlyers(query, limit);
}

@Query((_return) => [Flyer], {
nullable: false,
description: `Returns a list of <Flyers> of size <limit> given a <categorySlug>, sorted by start date descending. Results can be offsetted by <offset> >= 0. Default <limit> is ${DEFAULT_LIMIT}`,
})
async getFlyersByCategorySlug(
@Arg('categorySlug') categorySlug: string,
@Arg('limit', { defaultValue: DEFAULT_LIMIT }) limit: number,
@Arg('offset', { defaultValue: DEFAULT_OFFSET }) offset: number,
) {
return FlyerRepo.getFlyersByCategorySlug(categorySlug, limit, offset);
}

@FieldResolver((_returns) => Number, { description: 'The trendiness score of a <Flyer>' })
async trendiness(@Root() flyer: Flyer): Promise<number> {
const presentDate = new Date().getTime();
Expand All @@ -136,13 +148,6 @@ class FlyerResolver {
return flyer['_doc'].timesClicked / ((presentDate - flyer['_doc'].startDate.getTime()) / 1000); // eslint-disable-line
}

@FieldResolver((_returns) => Boolean, {
description: 'If an <Flyer> contains not suitable for work content',
})
async nsfw(@Root() flyer: Flyer): Promise<boolean> {
return FlyerRepo.checkProfanity(flyer['_doc'].title); //eslint-disable-line
}

@Mutation((_returns) => Flyer, {
nullable: true,
description: `Increments the times clicked of a <Flyer> with the given <id>.`,
Expand Down
39 changes: 39 additions & 0 deletions src/tests/flyer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,42 @@ describe('getTrending tests', () => {
expect(getFlyersResponse).toHaveLength(5);
});
});

describe('getFlyersByCategorySlug tests', () => {
test('query flyer with invalid slug', async () => {
const flyers = await FlyerFactory.create(2);
await FlyerModel.insertMany(flyers);

const getFlyersResponse = await FlyerRepo.getFlyersByCategorySlug(Math.random().toString());
expect(getFlyersResponse).toHaveLength(0);
});

test('query flyer with existing slug', async () => {
const flyers = await FlyerFactory.create(4);
await FlyerModel.insertMany(flyers);

const randomSlug = Math.random().toString();
const specificFlyer = await FlyerFactory.createSpecific(1, { categorySlug: randomSlug });
await FlyerModel.insertMany(specificFlyer);

const getFlyersResponse = await FlyerRepo.getFlyersByCategorySlug(randomSlug);
expect(getFlyersResponse[0].categorySlug).toEqual(specificFlyer[0].categorySlug);
expect(getFlyersResponse).toHaveLength(1);
});

test('query multiple flyers with existing slug', async () => {
const flyers = await FlyerFactory.create(4);
await FlyerModel.insertMany(flyers);

const randomSlug = Math.random().toString();
const specificFlyers = await FlyerFactory.createSpecific(4, { categorySlug: randomSlug });
await FlyerModel.insertMany(specificFlyers);

const limit = 2;
const getFlyersResponse = await FlyerRepo.getFlyersByCategorySlug(
specificFlyers[0].categorySlug,
limit,
);
expect(getFlyersResponse).toHaveLength(limit);
});
});

0 comments on commit d49b504

Please sign in to comment.