From 2200473cd581da875a9bc07232ad3f573a618af0 Mon Sep 17 00:00:00 2001 From: Anders Rasmussen Date: Wed, 11 Oct 2023 14:31:16 +0200 Subject: [PATCH] #17 - Added changes according to feedback --- .../migration.sql | 2 + prisma/schema.prisma | 3 + src/locales/da.json | 4 + src/locales/en.json | 4 + src/modules/item/__test__/item.read.test.ts | 4 + src/modules/item/__test__/item.root.test.ts | 5 + .../item/__test__/item.service.test.ts | 15 ++ .../item/__test__/item.starred.test.ts | 26 ++- src/modules/item/item.schema.ts | 10 +- src/modules/item/item.service.ts | 19 +- src/modules/item/starred/__test__/add.test.ts | 6 +- .../item/starred/__test__/delete.test.ts | 59 ++++- .../item/starred/__test__/read.test.ts | 203 ------------------ .../item/starred/starred.controller.ts | 35 +-- src/modules/item/starred/starred.route.ts | 20 -- src/modules/item/starred/starred.schema.ts | 80 +------ src/modules/item/starred/starred.service.ts | 14 -- 17 files changed, 140 insertions(+), 369 deletions(-) rename prisma/migrations/{20231010082448_item_starred => 20231011083919_item_starred}/migration.sql (88%) delete mode 100644 src/modules/item/starred/__test__/read.test.ts diff --git a/prisma/migrations/20231010082448_item_starred/migration.sql b/prisma/migrations/20231011083919_item_starred/migration.sql similarity index 88% rename from prisma/migrations/20231010082448_item_starred/migration.sql rename to prisma/migrations/20231011083919_item_starred/migration.sql index 9ca16bf..cffde7a 100644 --- a/prisma/migrations/20231010082448_item_starred/migration.sql +++ b/prisma/migrations/20231011083919_item_starred/migration.sql @@ -3,6 +3,8 @@ CREATE TABLE "ItemStarred" ( "id" SERIAL NOT NULL, "itemId" INTEGER NOT NULL, "userId" INTEGER NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, CONSTRAINT "ItemStarred_pkey" PRIMARY KEY ("id") ); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 80d8200..ba1527b 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -129,6 +129,9 @@ model ItemStarred { item Item @relation(fields: [itemId], references: [id], onDelete: Cascade) user User @relation(fields: [userId], references: [id], onDelete: Cascade) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + @@unique([itemId, userId]) @@index([itemId]) @@index([userId]) diff --git a/src/locales/da.json b/src/locales/da.json index df03305..de57f76 100644 --- a/src/locales/da.json +++ b/src/locales/da.json @@ -173,6 +173,10 @@ "required": "id er påkrævet", "type": "id skal være et tal" }, + "userId": { + "required": "userId er påkrævet", + "type": "userId skal være et tal" + }, "itemId": { "required": "itemId er påkrævet", "type": "itemId skal være et tal" diff --git a/src/locales/en.json b/src/locales/en.json index 82d49c8..7e2345f 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -173,6 +173,10 @@ "required": "id is required", "type": "id must be a number" }, + "userId": { + "required": "userId is required", + "type": "userId must be a number" + }, "itemId": { "required": "itemId is required", "type": "itemId must be a number" diff --git a/src/modules/item/__test__/item.read.test.ts b/src/modules/item/__test__/item.read.test.ts index d6870d0..b60fd2e 100644 --- a/src/modules/item/__test__/item.read.test.ts +++ b/src/modules/item/__test__/item.read.test.ts @@ -96,6 +96,7 @@ describe('GET /api/item/:parentId', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, { id: expect.any(Number), @@ -107,6 +108,7 @@ describe('GET /api/item/:parentId', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, { id: expect.any(Number), @@ -118,6 +120,7 @@ describe('GET /api/item/:parentId', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, { id: expect.any(Number), @@ -128,6 +131,7 @@ describe('GET /api/item/:parentId', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, ]); }); diff --git a/src/modules/item/__test__/item.root.test.ts b/src/modules/item/__test__/item.root.test.ts index 7af6645..79f0283 100644 --- a/src/modules/item/__test__/item.root.test.ts +++ b/src/modules/item/__test__/item.root.test.ts @@ -89,6 +89,7 @@ describe('GET /api/item', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, { id: expect.any(Number), @@ -100,6 +101,7 @@ describe('GET /api/item', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, { id: expect.any(Number), @@ -111,6 +113,7 @@ describe('GET /api/item', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, { id: expect.any(Number), @@ -122,6 +125,7 @@ describe('GET /api/item', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, { id: expect.any(Number), @@ -132,6 +136,7 @@ describe('GET /api/item', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: false, }, ]); }); diff --git a/src/modules/item/__test__/item.service.test.ts b/src/modules/item/__test__/item.service.test.ts index 2bae2f2..c4b542f 100644 --- a/src/modules/item/__test__/item.service.test.ts +++ b/src/modules/item/__test__/item.service.test.ts @@ -82,6 +82,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -94,6 +95,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -106,6 +108,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -118,6 +121,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, ]); }); @@ -247,6 +251,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -259,6 +264,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -271,6 +277,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -283,6 +290,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -295,6 +303,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -307,6 +316,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -319,6 +329,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, ]; const expectedSharredUser = [ @@ -333,6 +344,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -345,6 +357,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -357,6 +370,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, { id: expect.any(Number), @@ -369,6 +383,7 @@ describe('ItemService', () => { createdAt: expect.any(Date), deletedAt: null, updatedAt: expect.any(Date), + isStarred: false, }, ]; diff --git a/src/modules/item/__test__/item.starred.test.ts b/src/modules/item/__test__/item.starred.test.ts index f8f93cd..7b04dd2 100644 --- a/src/modules/item/__test__/item.starred.test.ts +++ b/src/modules/item/__test__/item.starred.test.ts @@ -134,6 +134,19 @@ describe('GET /api/item/starred', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: true, + }, + { + id: blob.id, + name: 'test1.txt', + blobUrl: 'https://example.com/test1.txt', + parentId: null, + ownerId: user.id, + mimeType: 'text/plain', + createdAt: expect.any(String), + deletedAt: null, + updatedAt: expect.any(String), + isStarred: true, }, { id: blob2.id, @@ -145,6 +158,7 @@ describe('GET /api/item/starred', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), + isStarred: true, }, { id: folder2.id, @@ -156,17 +170,7 @@ describe('GET /api/item/starred', () => { createdAt: expect.any(String), deletedAt: null, updatedAt: expect.any(String), - }, - { - id: blob.id, - name: 'test1.txt', - blobUrl: 'https://example.com/test1.txt', - parentId: null, - ownerId: user.id, - mimeType: 'text/plain', - createdAt: expect.any(String), - deletedAt: null, - updatedAt: expect.any(String), + isStarred: true, }, ]); }); diff --git a/src/modules/item/item.schema.ts b/src/modules/item/item.schema.ts index 1d4eb2a..b959235 100644 --- a/src/modules/item/item.schema.ts +++ b/src/modules/item/item.schema.ts @@ -4,6 +4,7 @@ import { ItemFolder as prismaItemFolderType, ItemDocs as prismaItemDocsType, ItemShortcut as prismaItemShortcutType, + ItemStarred as prismaItemStarredType, } from '@prisma/client'; import { FromSchema } from 'json-schema-to-ts'; @@ -11,13 +12,15 @@ export type Item = prismaItemType; export type ItemPrismaProperties = Item & { ItemBlob: prismaItemBlobType | null } & { ItemFolder: prismaItemFolderType | null; -} & { ItemDocs: prismaItemDocsType | null } & { ItemShortcut: prismaItemShortcutType | null }; +} & { ItemDocs: prismaItemDocsType | null } & { ItemShortcut: prismaItemShortcutType | null } & { + ItemStarred: prismaItemStarredType[]; +}; export type ItemWithProperties = Item & Omit, 'id' | 'itemId'> & Omit, 'id' | 'itemId'> & Omit, 'id' | 'itemId'> & - Omit, 'id' | 'itemId'>; + Omit, 'id' | 'itemId'> & { isStarred: boolean }; export type CreateItem = Omit; @@ -66,6 +69,9 @@ const itemsResponseSchema = { parentId: { type: ['number', 'null'], }, + isStarred: { + type: 'boolean', + }, mimeType: { type: 'string', }, diff --git a/src/modules/item/item.service.ts b/src/modules/item/item.service.ts index 9a3ee4a..ebc9a98 100644 --- a/src/modules/item/item.service.ts +++ b/src/modules/item/item.service.ts @@ -43,6 +43,11 @@ export default class ItemService { ItemFolder: true, ItemDocs: true, ItemShortcut: true, + ItemStarred: { + where: { + userId: ownerId, + }, + }, }, }); @@ -89,6 +94,11 @@ export default class ItemService { ItemFolder: true, ItemDocs: true, ItemShortcut: true, + ItemStarred: { + where: { + userId: userId, + }, + }, }, }); @@ -120,6 +130,11 @@ export default class ItemService { ItemFolder: true, ItemDocs: true, ItemShortcut: true, + ItemStarred: { + where: { + userId: userId, + }, + }, }, }); @@ -128,7 +143,8 @@ export default class ItemService { private formatItems(items: ItemPrismaProperties[]): ItemWithProperties[] { return items.map((element) => { - const { ItemFolder, ItemBlob, ItemDocs, ItemShortcut, ...strippedElement } = element; + const { ItemFolder, ItemBlob, ItemDocs, ItemShortcut, ItemStarred, ...strippedElement } = + element; return { ...ItemBlob, @@ -136,6 +152,7 @@ export default class ItemService { ...ItemDocs, ...ItemShortcut, ...strippedElement, + isStarred: ItemStarred.length > 0, }; }); } diff --git a/src/modules/item/starred/__test__/add.test.ts b/src/modules/item/starred/__test__/add.test.ts index ebe00cf..4faf127 100644 --- a/src/modules/item/starred/__test__/add.test.ts +++ b/src/modules/item/starred/__test__/add.test.ts @@ -61,10 +61,12 @@ describe('POST /api/starred', () => { id: expect.any(Number), userId: user.id, itemId: folder.id, + createdAt: expect.any(String), + updatedAt: expect.any(String), }); }); - it('should return status 200 and item, when sharred', async () => { + it('should return status 200 and item, when starred', async () => { const { accessToken } = await authService.createTokens(user.id); const folder = await folderService.createFolder({ @@ -98,6 +100,8 @@ describe('POST /api/starred', () => { id: expect.any(Number), itemId: folder.id, userId: user.id, + createdAt: expect.any(String), + updatedAt: expect.any(String), }); }); diff --git a/src/modules/item/starred/__test__/delete.test.ts b/src/modules/item/starred/__test__/delete.test.ts index 87d92f2..bade754 100644 --- a/src/modules/item/starred/__test__/delete.test.ts +++ b/src/modules/item/starred/__test__/delete.test.ts @@ -3,12 +3,15 @@ import UserService from '../../../auth/user.service'; import AuthService from '../../../auth/auth.service'; import StarredService from '../starred.service'; import FolderService from '../../folder/folder.service'; +import SharingService from '../../sharing/sharing.service'; +import ItemService from '../../item.service'; describe('DELETE /api/starred/:id', () => { let userService: UserService; let authService: AuthService; let starredService: StarredService; let folderService: FolderService; + let sharingService: SharingService; let user: User; let otherUser: User; @@ -18,6 +21,7 @@ describe('DELETE /api/starred/:id', () => { userService = new UserService(); starredService = new StarredService(); folderService = new FolderService(); + sharingService = new SharingService(new ItemService()); user = await userService.createUser({ name: 'Joe Biden the 1st', @@ -48,7 +52,7 @@ describe('DELETE /api/starred/:id', () => { const response = await global.fastify.inject({ method: 'DELETE', - url: '/api/starred/' + starred.id, + url: '/api/starred/' + starred.itemId, headers: { authorization: 'Bearer ' + accessToken, }, @@ -57,7 +61,7 @@ describe('DELETE /api/starred/:id', () => { expect(response.statusCode).toBe(204); expect(response.body).toBe(''); - await expect(starredService.getById(starred.id)).rejects.toThrowError(); + await expect(starredService.getByItemIdAndUserId(folder.id, user.id)).rejects.toThrowError(); }); it('should return status 401, when unauthorized', async () => { @@ -75,7 +79,7 @@ describe('DELETE /api/starred/:id', () => { const response = await global.fastify.inject({ method: 'DELETE', - url: '/api/starred/' + starred.id, + url: '/api/starred/' + starred.itemId, headers: { authorization: 'invalid_access_token!!!', }, @@ -108,20 +112,59 @@ describe('DELETE /api/starred/:id', () => { const response = await global.fastify.inject({ method: 'DELETE', - url: '/api/starred/' + starred.id, + url: '/api/starred/' + starred.itemId, headers: { authorization: 'Bearer ' + accessToken, }, }); - expect(response.statusCode).toBe(401); + expect(response.statusCode).toBe(400); expect(response.json()).toEqual({ - error: 'UnauthorizedError', + error: 'BadRequestError', errors: { - _: ['Unauthorized'], + _: ['Starred not found'], }, - statusCode: 401, + statusCode: 400, + }); + }); + + it('should return status 200, when removing a starring, to an item you no longer have access to', async () => { + const { accessToken } = await authService.createTokens(user.id); + + const folder = await folderService.createFolder({ + name: 'Folder1', + ownerId: otherUser.id, + parentId: null, + color: '#78BC61', }); + + const sharing = await sharingService.createSharing( + { + itemId: folder.id, + userId: user.id, + }, + otherUser.id, + ); + + await starredService.createStarred({ + itemId: folder.id, + userId: user.id, + }); + + await sharingService.deleteSharingByIdAndUserId(sharing.id, user.id); + + const response = await global.fastify.inject({ + method: 'DELETE', + url: '/api/starred/' + folder.id, + headers: { + authorization: 'Bearer ' + accessToken, + }, + }); + + expect(response.statusCode).toBe(204); + expect(response.body).toBe(''); + + await expect(starredService.getByItemIdAndUserId(folder.id, user.id)).rejects.toThrowError(); }); it("should return status 400, when starred id isn't a number", async () => { diff --git a/src/modules/item/starred/__test__/read.test.ts b/src/modules/item/starred/__test__/read.test.ts deleted file mode 100644 index 2e8b78a..0000000 --- a/src/modules/item/starred/__test__/read.test.ts +++ /dev/null @@ -1,203 +0,0 @@ -import { User } from '@prisma/client'; -import UserService from '../../../auth/user.service'; -import AuthService from '../../../auth/auth.service'; -import StarredService from '../starred.service'; -import FolderService from '../../folder/folder.service'; - -describe('GET /api/starred/:id', () => { - let userService: UserService; - let authService: AuthService; - let starredService: StarredService; - let folderService: FolderService; - - let user: User; - let otherUser: User; - - beforeAll(async () => { - authService = new AuthService(); - userService = new UserService(); - starredService = new StarredService(); - folderService = new FolderService(); - - user = await userService.createUser({ - name: 'Joe Biden the 1st', - email: 'joe@biden.com', - password: '1234', - }); - otherUser = await userService.createUser({ - name: 'Joe Biden the 2nd', - email: 'joe2@biden.com', - password: '4321', - }); - }); - - it('should return status 200 and starred', async () => { - const { accessToken } = await authService.createTokens(user.id); - - const folder = await folderService.createFolder({ - name: 'Folder1', - ownerId: user.id, - parentId: null, - color: '#78BC61', - }); - - const starred = await starredService.createStarred({ - itemId: folder.id, - userId: user.id, - }); - - const response = await global.fastify.inject({ - method: 'GET', - url: '/api/starred/' + starred.id, - headers: { - authorization: 'Bearer ' + accessToken, - }, - }); - - expect(response.statusCode).toBe(200); - expect(response.json()).toEqual({ - id: starred.id, - itemId: folder.id, - userId: user.id, - }); - }); - - it('should return status 401, when unauthorized', async () => { - const folder = await folderService.createFolder({ - name: 'Folder1', - ownerId: user.id, - parentId: null, - color: '#78BC61', - }); - - const starred = await starredService.createStarred({ - itemId: folder.id, - userId: user.id, - }); - - const response = await global.fastify.inject({ - method: 'GET', - url: '/api/starred/' + starred.id, - headers: { - authorization: 'invalid_access_token!!!', - }, - }); - - expect(response.statusCode).toBe(401); - expect(response.json()).toEqual({ - error: 'UnauthorizedError', - errors: { - _: ['Unauthorized'], - }, - statusCode: 401, - }); - }); - - it('should return status 400, since starred id is not accessible to others', async () => { - const { accessToken } = await authService.createTokens(user.id); - - const folder = await folderService.createFolder({ - name: 'Folder1', - ownerId: otherUser.id, - parentId: null, - color: '#78BC61', - }); - - const starred = await starredService.createStarred({ - itemId: folder.id, - userId: otherUser.id, - }); - - const response = await global.fastify.inject({ - method: 'GET', - url: '/api/starred/' + starred.id, - headers: { - authorization: 'Bearer ' + accessToken, - }, - }); - - expect(response.statusCode).toBe(401); - expect(response.json()).toEqual({ - error: 'UnauthorizedError', - errors: { - _: ['Unauthorized'], - }, - statusCode: 401, - }); - }); - - it('should return status 401, when starred id is not accessible to you', async () => { - const { accessToken } = await authService.createTokens(user.id); - - const folder = await folderService.createFolder({ - name: 'Folder1', - ownerId: otherUser.id, - parentId: null, - color: '#78BC61', - }); - - const starred = await starredService.createStarred({ - itemId: folder.id, - userId: user.id, - }); - - const response = await global.fastify.inject({ - method: 'GET', - url: '/api/starred/' + starred.id, - headers: { - authorization: 'Bearer ' + accessToken, - }, - }); - - expect(response.statusCode).toBe(401); - expect(response.json()).toEqual({ - error: 'UnauthorizedError', - errors: { - _: ['Unauthorized'], - }, - statusCode: 401, - }); - }); - - it("should return status 400, when starred id isn't a number", async () => { - const { accessToken } = await authService.createTokens(user.id); - - const response = await global.fastify.inject({ - method: 'GET', - url: '/api/starred/invalid_id', - headers: { - authorization: 'Bearer ' + accessToken, - }, - }); - - expect(response.statusCode).toBe(400); - expect(response.json()).toEqual({ - error: 'ValidationError', - errors: { - id: ['id must be a number'], - }, - statusCode: 400, - }); - }); - - it("should return status 400, when starred with id doesn't exist", async () => { - const { accessToken } = await authService.createTokens(user.id); - - const response = await global.fastify.inject({ - method: 'GET', - url: '/api/starred/1234', - headers: { - authorization: 'Bearer ' + accessToken, - }, - }); - - expect(response.statusCode).toBe(400); - expect(response.json()).toEqual({ - error: 'BadRequestError', - errors: { - _: ['Starred not found'], - }, - statusCode: 400, - }); - }); -}); diff --git a/src/modules/item/starred/starred.controller.ts b/src/modules/item/starred/starred.controller.ts index 2e9c62b..3b0df06 100644 --- a/src/modules/item/starred/starred.controller.ts +++ b/src/modules/item/starred/starred.controller.ts @@ -1,5 +1,5 @@ import { FastifyReply, FastifyRequest } from 'fastify'; -import { ReadInput, AddInput, DeleteInput } from './starred.schema'; +import { AddInput, DeleteInput } from './starred.schema'; import StarredService from './starred.service'; import AccessService from '../sharing/access.service'; @@ -12,30 +12,6 @@ export default class StarredController { this.accessService = accessService; } - public async readHandler( - request: FastifyRequest<{ - Params: ReadInput; - }>, - reply: FastifyReply, - ) { - try { - const starred = await this.starredService.getById(request.params.id); - - if (!(await this.accessService.hasAccessToItem(starred.id, request.user.sub))) { - return reply.unauthorized(); - } - - return reply.code(200).send(starred); - } catch (e) { - if (e instanceof Error) { - return reply.badRequest(request.i18n.t(e.message)); - } - - /* istanbul ignore next */ - return reply.badRequest(); - } - } - public async addHandler( request: FastifyRequest<{ Body: AddInput; @@ -70,11 +46,10 @@ export default class StarredController { reply: FastifyReply, ) { try { - const starred = await this.starredService.getById(request.params.id); - - if (!(await this.accessService.hasAccessToItem(starred.id, request.user.sub))) { - return reply.unauthorized(); - } + const starred = await this.starredService.getByItemIdAndUserId( + request.params.id, + request.user.sub, + ); await this.starredService.deleteStarredById(starred.id); return reply.code(204).send(); diff --git a/src/modules/item/starred/starred.route.ts b/src/modules/item/starred/starred.route.ts index 700e90f..05808e3 100644 --- a/src/modules/item/starred/starred.route.ts +++ b/src/modules/item/starred/starred.route.ts @@ -13,26 +13,6 @@ export default async (fastify: FastifyInstance) => { new AccessService(itemService, new SharingService(itemService)), ); - fastify.get( - '/:id', - { - schema: { - tags: ['Starred'], - params: { $ref: 'readStarredSchema' }, - response: { - 200: { $ref: 'readStarredResponseSchema' }, - }, - security: [ - { - bearerAuth: [], - }, - ], - }, - onRequest: [fastify.authenticate], - }, - starredController.readHandler.bind(starredController), - ); - fastify.post( '/', { diff --git a/src/modules/item/starred/starred.schema.ts b/src/modules/item/starred/starred.schema.ts index 23ed4cb..926acbc 100644 --- a/src/modules/item/starred/starred.schema.ts +++ b/src/modules/item/starred/starred.schema.ts @@ -2,62 +2,6 @@ import { FromSchema } from 'json-schema-to-ts'; import { Item } from '../item.schema'; import { ItemStarred as prismaItemStarredType } from '@prisma/client'; -const readStarredSchema = { - $id: 'readStarredSchema', - type: 'object', - properties: { - id: { - type: 'number', - errorMessage: { - type: 'starred.id.type', - }, - }, - }, - required: ['id'], - errorMessage: { - required: { - id: 'starred.id.required', - }, - }, -} as const; - -const readStarredResponseSchema = { - $id: 'readStarredResponseSchema', - type: 'object', - properties: { - id: { - type: 'number', - }, - userId: { - type: 'number', - }, - itemId: { - type: 'number', - }, - parentId: { - type: ['number', 'null'], - }, - name: { - type: 'string', - }, - mimeType: { - type: 'string', - }, - ownerId: { - type: 'number', - }, - deletedAt: { - type: ['string', 'null'], - }, - createdAt: { - type: 'string', - }, - updatedAt: { - type: 'string', - }, - }, -} as const; - const addStarredSchema = { $id: 'addStarredSchema', type: 'object', @@ -90,21 +34,6 @@ const addStarredResponseSchema = { itemId: { type: 'number', }, - name: { - type: 'string', - }, - parentId: { - type: ['number', 'null'], - }, - mimeType: { - type: 'string', - }, - ownerId: { - type: 'number', - }, - deletedAt: { - type: ['string', 'null'], - }, createdAt: { type: 'string', }, @@ -134,7 +63,6 @@ const deleteStarredSchema = { } as const; export type AddInput = FromSchema; -export type ReadInput = FromSchema; export type DeleteInput = FromSchema; export type AddStarred = { @@ -145,10 +73,4 @@ export type AddStarred = { export type ItemStarred = prismaItemStarredType & { item: Item }; export type Starred = prismaItemStarredType; -export const starredSchemas = [ - readStarredSchema, - readStarredResponseSchema, - addStarredSchema, - addStarredResponseSchema, - deleteStarredSchema, -]; +export const starredSchemas = [addStarredSchema, addStarredResponseSchema, deleteStarredSchema]; diff --git a/src/modules/item/starred/starred.service.ts b/src/modules/item/starred/starred.service.ts index 94168f0..aa9060f 100644 --- a/src/modules/item/starred/starred.service.ts +++ b/src/modules/item/starred/starred.service.ts @@ -31,20 +31,6 @@ export default class StarredService { } } - public async getById(id: number): Promise { - const itemStarred = await prisma.itemStarred.findUnique({ - where: { - id, - }, - }); - - if (!itemStarred) { - throw new Error('item.starred.notFound'); - } - - return itemStarred; - } - public async getByItemIdAndUserId(itemId: number, userId: number): Promise { const itemStarred = await prisma.itemStarred.findUnique({ where: {