Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added shortcut endpoints #24

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions prisma/migrations/20231009104422_item_shortcut/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- CreateTable
CREATE TABLE "ItemShortcut" (
"id" SERIAL NOT NULL,
"itemId" INTEGER NOT NULL,
"linkedItemId" INTEGER NOT NULL,

CONSTRAINT "ItemShortcut_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
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");

-- AddForeignKey
ALTER TABLE "ItemShortcut" ADD CONSTRAINT "ItemShortcut_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ItemShortcut" ADD CONSTRAINT "ItemShortcut_linkedItemId_fkey" FOREIGN KEY ("linkedItemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;
25 changes: 19 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ model Item {
ownerId Int
parentId Int?

owner User @relation(fields: [ownerId], references: [id])
parentItem Item? @relation("ItemToItem", fields: [parentId], references: [id])
Items Item[] @relation("ItemToItem")
ItemFolder ItemFolder?
ItemBlob ItemBlob?
owner User @relation(fields: [ownerId], references: [id])
parentItem Item? @relation("ItemToItem", fields: [parentId], references: [id])
Items Item[] @relation("ItemToItem")
ItemFolder ItemFolder?
ItemBlob ItemBlob?
ItemDocs ItemDocs?
ItemSharing ItemSharing[]
ItemSharing ItemSharing[]
ItemShortcut ItemShortcut? @relation("shortcutItem")
LinkedItemShortcut ItemShortcut? @relation("linkedItem")

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down Expand Up @@ -105,3 +107,14 @@ model ItemSharing {
@@index([itemId])
@@index([userId])
}

model ItemShortcut {
id Int @id @default(autoincrement())
itemId Int @unique
linkedItemId Int @unique
Anders164a marked this conversation as resolved.
Show resolved Hide resolved

shortcutItem Item @relation("shortcutItem", fields: [itemId], references: [id], onDelete: Cascade)
linkedItem Item @relation("linkedItem", fields: [linkedItemId], references: [id], onDelete: Cascade)

@@unique([itemId, linkedItemId])
Anders164a marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 20 additions & 0 deletions src/locales/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
"docs": {
"notFound": "Dokumentet blev ikke fundet"
},
"shortcut": {
"notFound": "Genvej blev ikke fundet"
},
"notFound": "Item ikke fundet"
},
"folder": {
Expand Down Expand Up @@ -143,5 +146,22 @@
"parentId": {
"type": "Parent id skal være et tal"
}
},
"shortcut": {
"id": {
"required": "id er påkrævet",
"type": "id skal være et tal"
},
"name": {
"required": "Navn er påkrævet",
"type": "Navn skal være en tekst"
},
"linkedItemId": {
"required": "Det forbundet itemId er påkrævet",
"type": "Det forbundet itemId skal være en tekst"
},
"parentId": {
"type": "Parent id skal være et tal"
}
}
}
20 changes: 20 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
"docs": {
"notFound": "Docs not found"
},
"shortcut": {
"notFound": "Shortcut not found"
},
"notFound": "Item not found"
},
"folder": {
Expand Down Expand Up @@ -143,5 +146,22 @@
"parentId": {
"type": "Parent id must be a number"
}
},
"shortcut": {
"id": {
"required": "id is required",
"type": "id must be a number"
},
"name": {
"required": "Name is required",
"type": "Name must be a string"
},
"linkedItemId": {
"required": "Linked itemId is required",
"type": "Linked itemId must be a number"
},
"parentId": {
"type": "Parent id must be a number"
}
}
}
31 changes: 29 additions & 2 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import FolderService from '../folder/folder.service';
import AuthService from '../../auth/auth.service';
import BlobService from '../blob/blob.service';
import DocsService from '../docs/docs.service';
import ShortcutService from '../shortcut/shortcut.service';

describe('GET /api/item/:parentId', () => {
let userService: UserService;
let folderService: FolderService;
let authService: AuthService;
let blobService: BlobService;
let docsService: DocsService;
let shortcutService: ShortcutService;

let user: User;
let otherUser: User;
Expand All @@ -21,6 +23,7 @@ describe('GET /api/item/:parentId', () => {
authService = new AuthService();
blobService = new BlobService();
docsService = new DocsService();
shortcutService = new ShortcutService();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand All @@ -44,7 +47,7 @@ describe('GET /api/item/:parentId', () => {
parentId: null,
});

await blobService.createBlob({
const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
Expand All @@ -66,6 +69,13 @@ describe('GET /api/item/:parentId', () => {
parentId: parentFolder.id,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: blob.id,
parentId: parentFolder.id,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + parentFolder.id,
Expand Down Expand Up @@ -109,6 +119,16 @@ describe('GET /api/item/:parentId', () => {
deletedAt: null,
updatedAt: expect.any(String),
},
{
id: expect.any(Number),
name: 'Shortcut',
parentId: parentFolder.id,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.shortcut',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
},
]);
});

Expand All @@ -120,7 +140,7 @@ describe('GET /api/item/:parentId', () => {
parentId: null,
});

await blobService.createBlob({
const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
Expand All @@ -134,6 +154,13 @@ describe('GET /api/item/:parentId', () => {
parentId: parentFolder.id,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: blob.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + parentFolder.id,
Expand Down
31 changes: 29 additions & 2 deletions src/modules/item/__test__/item.root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import FolderService from '../folder/folder.service';
import AuthService from '../../auth/auth.service';
import BlobService from '../blob/blob.service';
import DocsService from '../docs/docs.service';
import ShortcutService from '../shortcut/shortcut.service';

describe('GET /api/item', () => {
let userService: UserService;
let folderService: FolderService;
let authService: AuthService;
let blobService: BlobService;
let docsService: DocsService;
let shortcutService: ShortcutService;

let user: User;

Expand All @@ -20,6 +22,7 @@ describe('GET /api/item', () => {
authService = new AuthService();
blobService = new BlobService();
docsService = new DocsService();
shortcutService = new ShortcutService();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand All @@ -39,7 +42,7 @@ describe('GET /api/item', () => {
blobUrl: 'https://example.com/test1.txt',
});

await folderService.createFolder({
const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: user.id,
Expand All @@ -59,6 +62,13 @@ describe('GET /api/item', () => {
parentId: null,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: folder.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item',
Expand Down Expand Up @@ -113,6 +123,16 @@ describe('GET /api/item', () => {
deletedAt: null,
updatedAt: expect.any(String),
},
{
id: expect.any(Number),
name: 'Shortcut',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.shortcut',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
},
]);
});

Expand All @@ -125,7 +145,7 @@ describe('GET /api/item', () => {
blobUrl: 'https://example.com/test1.txt',
});

await folderService.createFolder({
const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: user.id,
Expand All @@ -138,6 +158,13 @@ describe('GET /api/item', () => {
parentId: null,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: folder.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item',
Expand Down
Loading