diff --git a/src/reaction/reaction.contoller.ts b/src/reaction/reaction.contoller.ts index 11f72ca..c6318e4 100644 --- a/src/reaction/reaction.contoller.ts +++ b/src/reaction/reaction.contoller.ts @@ -31,7 +31,7 @@ export class ReactionController { } static async getOne(req: Request, res: Response) { - const reaction = await ReactionManager.getOne(req.query); + const reaction = await ReactionManager.getOne(req.query.resource, req.query.user); if (!reaction) { throw new ReactionNotFoundError(); } diff --git a/src/reaction/reaction.manager.ts b/src/reaction/reaction.manager.ts index bedb55b..06702d4 100644 --- a/src/reaction/reaction.manager.ts +++ b/src/reaction/reaction.manager.ts @@ -4,10 +4,7 @@ import { ReactionRepository } from './reaction.repository'; export class ReactionManager implements ReactionRepository { static async create(reaction: IReaction) { const existingReaction: IReaction | null = - await ReactionManager.getOne({ - user: reaction.user, - resource: reaction.resource, - }); + await ReactionManager.getOne(reaction.user, reaction.resource); if (existingReaction) { return Promise.resolve(existingReaction); @@ -28,8 +25,8 @@ export class ReactionManager implements ReactionRepository { return ReactionRepository.deleteMany(resource); } - static getOne(reactionFilter: Partial) { - return ReactionRepository.getOne(reactionFilter); + static getOne(resource: string, user: string) { + return ReactionRepository.getOne(resource, user); } static getMany(reactionFilter: Partial) { diff --git a/src/reaction/reaction.repository.spec.ts b/src/reaction/reaction.repository.spec.ts index a6efad6..0f6a6d3 100644 --- a/src/reaction/reaction.repository.spec.ts +++ b/src/reaction/reaction.repository.spec.ts @@ -71,6 +71,7 @@ const reactionFilter: Partial = { const reactionDataToUpdate: Partial = { type: ReactionType.Dislike }; const unexistingReaction: Partial = { resource: '5bf54919902f5a46a0fb2e33', + user: 'aaaa@vavaav', }; const unknownProperty: Object = { unknownProperty: true }; @@ -287,7 +288,7 @@ describe('Reaction Repository', function () { expect(deleted).to.exist; expect(deleted).to.be.true; - const getDeleted = await ReactionRepository.getOne(reaction); + const getDeleted = await ReactionRepository.getOne(reaction.resource, reaction.user); expect(getDeleted).to.not.exist; }); @@ -325,8 +326,8 @@ describe('Reaction Repository', function () { expect(deleted).to.exist; expect(deleted).to.be.true; - const getDeleted = await ReactionRepository.getOne(reaction); - const getDeleted2 = await ReactionRepository.getOne(reaction2); + const getDeleted = await ReactionRepository.getOne(reaction.resource, reaction.user); + const getDeleted2 = await ReactionRepository.getOne(reaction2.resource, reaction2.user); expect(getDeleted).to.not.exist; expect(getDeleted2).to.not.exist; }); @@ -336,9 +337,9 @@ describe('Reaction Repository', function () { expect(deleted).to.exist; expect(deleted).to.be.true; - const getDeleted = await ReactionRepository.getOne(reaction); - const getDeleted2 = await ReactionRepository.getOne(reaction2); - const getDeleted3 = await ReactionRepository.getOne(reaction3); + const getDeleted = await ReactionRepository.getOne(reaction.resource, reaction.user); + const getDeleted2 = await ReactionRepository.getOne(reaction2.resource, reaction2.user); + const getDeleted3 = await ReactionRepository.getOne(reaction3.resource, reaction3.user); expect(getDeleted).to.not.exist; expect(getDeleted2).to.not.exist; expect(getDeleted3).to.not.exist; @@ -348,8 +349,8 @@ describe('Reaction Repository', function () { const res = await ReactionRepository.deleteMany(new mongoose.Types.ObjectId().toHexString()); expect(res).to.be.true; - const getReaction = await ReactionRepository.getOne(reaction); - const getReaction2 = await ReactionRepository.getOne(reaction2); + const getReaction = await ReactionRepository.getOne(reaction.resource, reaction.user); + const getReaction2 = await ReactionRepository.getOne(reaction2.resource, reaction2.user); expect(getReaction).to.exist; expect(getReaction2).to.exist; }); @@ -358,9 +359,9 @@ describe('Reaction Repository', function () { const res = await ReactionRepository.deleteMany([new mongoose.Types.ObjectId().toHexString(), new mongoose.Types.ObjectId().toHexString()]); expect(res).to.be.true; - const getReaction = await ReactionRepository.getOne(reaction); - const getReaction2 = await ReactionRepository.getOne(reaction2); - const getReaction3 = await ReactionRepository.getOne(reaction3); + const getReaction = await ReactionRepository.getOne(reaction.resource, reaction.user); + const getReaction2 = await ReactionRepository.getOne(reaction2.resource, reaction2.user); + const getReaction3 = await ReactionRepository.getOne(reaction3.resource, reaction3.user); expect(getReaction).to.exist; expect(getReaction2).to.exist; expect(getReaction3).to.exist; @@ -378,10 +379,7 @@ describe('Reaction Repository', function () { }); it('Should return document by id', async function () { - const returnedReaction = await ReactionRepository.getOne({ - resource: reaction.resource, - user: reaction.user, - } as Partial); + const returnedReaction = await ReactionRepository.getOne(reaction.resource, reaction.user); expect(returnedReaction).to.exist; @@ -390,30 +388,18 @@ describe('Reaction Repository', function () { } }); - it('Should return document by reactionType', async function () { - const createdReaction = await ReactionRepository.getOne({ - type: reaction.type, - } as Partial); - - expect(createdReaction).to.exist; - - for (const prop in reaction) { - expect(createdReaction).to.have.property(prop, reaction[prop as keyof IReaction]); - } - }); - it('Should return null when document does not exist', async function () { - const returnedReaction = await ReactionRepository.getOne(unexistingReaction); + const returnedReaction = await ReactionRepository.getOne(unexistingReaction.resource!, unexistingReaction.user!); expect(returnedReaction).to.not.exist; }); }); context('When data is invalid', function () { - it('Should throw error when filter does not exist', async function () { + it('Should throw error when resource and user does not exist', async function () { let hasThrown = false; try { - await ReactionRepository.getOne({}); + await ReactionRepository.getOne('', ''); } catch (err) { hasThrown = true; expect(err).to.exist; @@ -422,11 +408,6 @@ describe('Reaction Repository', function () { expect(hasThrown).to.be.true; } }); - - it('Should return null when filter is not in the correct format', async function () { - const doc = await ReactionRepository.getOne(unknownProperty); - expect(doc).to.not.exist; - }); }); }); diff --git a/src/reaction/reaction.repository.ts b/src/reaction/reaction.repository.ts index e0f6dd4..b777ba1 100644 --- a/src/reaction/reaction.repository.ts +++ b/src/reaction/reaction.repository.ts @@ -49,14 +49,15 @@ export class ReactionRepository { return Promise.resolve(response.ok === 1); } - static getOne(reactionFilter: Partial) + static getOne(resource: string, user: string) : Promise { - if (Object.keys(reactionFilter).length === 0) { - throw new ServerError('Filter is required.'); + if (!resource || !user) { + throw new ServerError('Resource and User are required.'); } - return ReactionModel.findOne( - reactionFilter, - ).exec(); + return ReactionModel.findOne({ + resource, + user, + }).exec(); } static getMany(reactionFilter: Partial) diff --git a/src/reaction/reaction.router.spec.ts b/src/reaction/reaction.router.spec.ts index 2d02693..43e792f 100644 --- a/src/reaction/reaction.router.spec.ts +++ b/src/reaction/reaction.router.spec.ts @@ -18,7 +18,7 @@ import { ReactionManager } from './reaction.manager'; import { sign } from 'jsonwebtoken'; import { ReactionRepository } from './reaction.repository'; -describe('Reaction Module', function () { +describe('Reaction Router', function () { let server: Server; const authorizationHeader = `Bearer ${sign('mock-user', config.authentication.secret)}`; @@ -79,8 +79,9 @@ describe('Reaction Module', function () { }; const reactionDataToUpdate: Partial = { type: ReactionType.Dislike }; - const unexistingReaction: Partial = { + const unexistingReaction = { resource: '5bf54919902f5a46a0fb2e33', + user: 'a@aaaab', }; const unknownProperty: Object = { unknownProperty: true }; @@ -329,7 +330,7 @@ describe('Reaction Module', function () { it('Should return error when reaction not found', function (done: MochaDone) { request(server.app) .get('/api/reaction/one') - .query(unexistingReaction) + .query({ resource: unexistingReaction.resource, user: unexistingReaction.user }) .set({ authorization: authorizationHeader }) .expect(404) .expect('Content-Type', /json/) diff --git a/src/reaction/validator/reaction.mocks.ts b/src/reaction/validator/reaction.mocks.ts index 1ba32a6..0fd34ff 100644 --- a/src/reaction/validator/reaction.mocks.ts +++ b/src/reaction/validator/reaction.mocks.ts @@ -62,7 +62,10 @@ export class ValidRequestMocks { headers: { authorization: this.authorizationHeader, }, - query: this.reaction, + query: { + resource: this.reaction.resource, + user: this.reaction.user, + }, }); getMany = createRequest({