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; const note = Note.create(user, alias) as Note;
it('with no note', async () => { 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); const notes = await service.getUserNotes(user);
expect(notes).toEqual([]); expect(notes).toEqual([]);
}); });
it('with one note', async () => { 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); const notes = await service.getUserNotes(user);
expect(notes).toEqual([note]); expect(notes).toEqual([note]);
}); });
it('with multiple note', async () => { 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); const notes = await service.getUserNotes(user);
expect(notes).toEqual([note, note]); expect(notes).toEqual([note, note]);
}); });

View file

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