fix(notes-service): user query builder

For reasons, the typeorm 0.3 broke the find()
method when using relations in the WHERE clause.
This replaces the find method with a query builder.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-04-03 17:37:38 +02:00
parent 27a93a2895
commit d780d511cd
2 changed files with 38 additions and 14 deletions

View file

@ -211,19 +211,49 @@ describe('NotesService', () => {
const note = Note.create(user, alias) as Note;
it('with no note', async () => {
jest.spyOn(noteRepo, 'find').mockResolvedValueOnce(null);
const createQueryBuilder = {
where: () => createQueryBuilder,
getMany: async () => {
return null;
},
};
jest
.spyOn(noteRepo, 'createQueryBuilder')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementation(() => createQueryBuilder);
const notes = await service.getUserNotes(user);
expect(notes).toEqual([]);
});
it('with one note', async () => {
jest.spyOn(noteRepo, 'find').mockResolvedValueOnce([note]);
const createQueryBuilder = {
where: () => createQueryBuilder,
getMany: async () => {
return [note];
},
};
jest
.spyOn(noteRepo, 'createQueryBuilder')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementation(() => createQueryBuilder);
const notes = await service.getUserNotes(user);
expect(notes).toEqual([note]);
});
it('with multiple note', async () => {
jest.spyOn(noteRepo, 'find').mockResolvedValueOnce([note, note]);
const createQueryBuilder = {
where: () => createQueryBuilder,
getMany: async () => {
return [note, note];
},
};
jest
.spyOn(noteRepo, 'createQueryBuilder')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementation(() => createQueryBuilder);
const notes = await service.getUserNotes(user);
expect(notes).toEqual([note, note]);
});

View file

@ -5,7 +5,7 @@
*/
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Equal, Repository } from 'typeorm';
import { Repository } from 'typeorm';
import noteConfiguration, { NoteConfig } from '../config/note.config';
import {
@ -55,16 +55,10 @@ export class NotesService {
* @return {Note[]} arary of notes owned by the user
*/
async getUserNotes(user: User): Promise<Note[]> {
const notes = await this.noteRepository.find({
where: { owner: Equal(user) },
relations: [
'owner',
'userPermissions',
'groupPermissions',
'tags',
'aliases',
],
});
const notes = await this.noteRepository
.createQueryBuilder('note')
.where('note.owner = :user', { user: user.id })
.getMany();
if (notes === null) {
return [];
}