Skip to content

Commit

Permalink
#10 - Added changes according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders164a committed Oct 9, 2023
1 parent 1ae551a commit 1c4c8dd
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ CREATE TABLE "ItemShortcut" (
CREATE UNIQUE INDEX "ItemShortcut_itemId_key" ON "ItemShortcut"("itemId");

-- CreateIndex
CREATE UNIQUE INDEX "ItemShortcut_linkedItemId_key" ON "ItemShortcut"("linkedItemId");

-- CreateIndex
CREATE UNIQUE INDEX "ItemShortcut_itemId_linkedItemId_key" ON "ItemShortcut"("itemId", "linkedItemId");
CREATE INDEX "ItemShortcut_linkedItemId_idx" ON "ItemShortcut"("linkedItemId");

-- AddForeignKey
ALTER TABLE "ItemShortcut" ADD CONSTRAINT "ItemShortcut_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Expand Down
10 changes: 5 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ model Item {
Items Item[] @relation("ItemToItem")
ItemFolder ItemFolder?
ItemBlob ItemBlob?
ItemDocs ItemDocs?
ItemDocs ItemDocs?
ItemSharing ItemSharing[]
ItemShortcut ItemShortcut? @relation("shortcutItem")
LinkedItemShortcut ItemShortcut? @relation("linkedItem")
LinkedItemShortcut ItemShortcut[] @relation("linkedItem")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down Expand Up @@ -111,10 +111,10 @@ model ItemSharing {
model ItemShortcut {
id Int @id @default(autoincrement())
itemId Int @unique
linkedItemId Int @unique
linkedItemId Int
shortcutItem Item @relation("shortcutItem", fields: [itemId], references: [id], onDelete: Cascade)
linkedItem Item @relation("linkedItem", fields: [linkedItemId], references: [id], onDelete: Cascade)
@@unique([itemId, linkedItemId])
@@index([linkedItemId])
}
60 changes: 60 additions & 0 deletions src/modules/item/shortcut/__test__/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('POST /api/shortcut', () => {
expect(response.json()).toEqual({
id: expect.any(Number),
name: 'Shortcut Folder',
linkedItemId: folder.id,
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.shortcut',
Expand All @@ -63,6 +64,65 @@ describe('POST /api/shortcut', () => {
});
});

it('should return status 200 and item, when adding a shortcut twice', async () => {
const { accessToken } = await authService.createTokens(user.id);

const folder = await folderService.createFolder({
name: 'Folder1',
ownerId: user.id,
parentId: null,
color: '#78BC61',
});

const response = await global.fastify.inject({
method: 'POST',
url: '/api/shortcut',
headers: {
authorization: 'Bearer ' + accessToken,
},
payload: {
name: 'Shortcut Folder',
linkedItemId: folder.id,
},
});

const response2 = await global.fastify.inject({
method: 'POST',
url: '/api/shortcut',
headers: {
authorization: 'Bearer ' + accessToken,
},
payload: {
name: 'Shortcut Folder2',
linkedItemId: folder.id,
},
});

expect(response.statusCode).toBe(200);
expect(response.json()).toEqual({
id: expect.any(Number),
name: 'Shortcut Folder',
parentId: null,
ownerId: user.id,
linkedItemId: folder.id,
mimeType: 'application/vnd.cloudstore.shortcut',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
});
expect(response2.json()).toEqual({
id: expect.any(Number),
name: 'Shortcut Folder2',
parentId: null,
ownerId: user.id,
linkedItemId: folder.id,
mimeType: 'application/vnd.cloudstore.shortcut',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
});
});

it('should return status 401, when unauthorized', async () => {
const folder = await folderService.createFolder({
name: 'Folder1',
Expand Down
1 change: 1 addition & 0 deletions src/modules/item/shortcut/__test__/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('PUT /api/shortcut', () => {
expect(response.json()).toEqual({
...shortcut,
name: shortcut.name + ' Updated',
linkedItemId: folder.id,
createdAt: shortcut.createdAt.toISOString(),
updatedAt: expect.any(String),
deletedAt: shortcut.deletedAt?.toISOString() ?? null,
Expand Down
1 change: 1 addition & 0 deletions src/modules/item/shortcut/__test__/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('GET /api/shortcut/:id', () => {
expect(response.statusCode).toBe(200);
expect(response.json()).toEqual({
...shortcut,
linkedItemId: folder.id,
createdAt: shortcut.createdAt.toISOString(),
updatedAt: shortcut.updatedAt.toISOString(),
deletedAt: shortcut.deletedAt?.toISOString() ?? null,
Expand Down
17 changes: 10 additions & 7 deletions src/modules/item/shortcut/shortcut.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ const readShortcutResponseSchema = {
id: {
type: 'number',
},
color: {
type: 'string',
},
parentId: {
type: ['number', 'null'],
},
linkedItemId: {
type: 'number',
},
name: {
type: 'string',
},
Expand Down Expand Up @@ -97,6 +97,9 @@ const editShortcutResponseSchema = {
parentId: {
type: ['number', 'null'],
},
linkedItemId: {
type: 'number',
},
name: {
type: 'string',
},
Expand Down Expand Up @@ -159,8 +162,8 @@ const addShortcutResponseSchema = {
name: {
type: 'string',
},
color: {
type: 'string',
linkedItemId: {
type: 'number',
},
parentId: {
type: ['number', 'null'],
Expand Down Expand Up @@ -214,8 +217,8 @@ export type AddShortcut = {
parentId: number | null;
};

export type ItemShortcut = prismaItemShortcutType & { shortcutItem: Item } & { linkedItem: Item };
export type Shortcut = Item;
export type ItemShortcut = prismaItemShortcutType & { shortcutItem: Item };
export type Shortcut = Omit<prismaItemShortcutType, 'id' | 'itemId'> & Item;
export type UpdateShortcut = { id: number } & Partial<AddShortcut> & Omit<UpdateItem, 'mimeType'>;

export const shortcutSchemas = [
Expand Down
4 changes: 1 addition & 3 deletions src/modules/item/shortcut/shortcut.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default class ShortcutService {
},
include: {
shortcutItem: true,
linkedItem: true,
},
});

Expand All @@ -35,7 +34,6 @@ export default class ShortcutService {
},
include: {
shortcutItem: true,
linkedItem: true,
},
});

Expand All @@ -61,7 +59,6 @@ export default class ShortcutService {
},
include: {
shortcutItem: true,
linkedItem: true,
},
});

Expand All @@ -78,6 +75,7 @@ export default class ShortcutService {

private formatItemShortcut(itemShortcut: ItemShortcut): Shortcut {
return {
linkedItemId: itemShortcut.linkedItemId,
...itemShortcut.shortcutItem,
};
}
Expand Down

0 comments on commit 1c4c8dd

Please sign in to comment.