Skip to content

Commit

Permalink
#17 - Added changes according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders164a committed Oct 11, 2023
1 parent 5a8d5cc commit 2200473
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
Expand Down
3 changes: 3 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 4 additions & 0 deletions src/locales/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -128,6 +131,7 @@ describe('GET /api/item/:parentId', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
},
]);
});
Expand Down
5 changes: 5 additions & 0 deletions src/modules/item/__test__/item.root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('GET /api/item', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -100,6 +101,7 @@ describe('GET /api/item', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -111,6 +113,7 @@ describe('GET /api/item', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -122,6 +125,7 @@ describe('GET /api/item', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -132,6 +136,7 @@ describe('GET /api/item', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
},
]);
});
Expand Down
15 changes: 15 additions & 0 deletions src/modules/item/__test__/item.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -94,6 +95,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -106,6 +108,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -118,6 +121,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
]);
});
Expand Down Expand Up @@ -247,6 +251,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -259,6 +264,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -271,6 +277,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -283,6 +290,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -295,6 +303,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -307,6 +316,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -319,6 +329,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
];
const expectedSharredUser = [
Expand All @@ -333,6 +344,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -345,6 +357,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -357,6 +370,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
{
id: expect.any(Number),
Expand All @@ -369,6 +383,7 @@ describe('ItemService', () => {
createdAt: expect.any(Date),
deletedAt: null,
updatedAt: expect.any(Date),
isStarred: false,
},
];

Expand Down
26 changes: 15 additions & 11 deletions src/modules/item/__test__/item.starred.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -145,6 +158,7 @@ describe('GET /api/item/starred', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
isStarred: true,
},
{
id: folder2.id,
Expand All @@ -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,
},
]);
});
Expand Down
10 changes: 8 additions & 2 deletions src/modules/item/item.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import {
ItemFolder as prismaItemFolderType,
ItemDocs as prismaItemDocsType,
ItemShortcut as prismaItemShortcutType,
ItemStarred as prismaItemStarredType,
} from '@prisma/client';
import { FromSchema } from 'json-schema-to-ts';

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<Partial<prismaItemBlobType>, 'id' | 'itemId'> &
Omit<Partial<prismaItemFolderType>, 'id' | 'itemId'> &
Omit<Partial<prismaItemDocsType>, 'id' | 'itemId'> &
Omit<Partial<prismaItemShortcutType>, 'id' | 'itemId'>;
Omit<Partial<prismaItemShortcutType>, 'id' | 'itemId'> & { isStarred: boolean };

export type CreateItem = Omit<Item, 'id' | 'createdAt' | 'updatedAt' | 'deletedAt'>;

Expand Down Expand Up @@ -66,6 +69,9 @@ const itemsResponseSchema = {
parentId: {
type: ['number', 'null'],
},
isStarred: {
type: 'boolean',
},
mimeType: {
type: 'string',
},
Expand Down
19 changes: 18 additions & 1 deletion src/modules/item/item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export default class ItemService {
ItemFolder: true,
ItemDocs: true,
ItemShortcut: true,
ItemStarred: {
where: {
userId: ownerId,
},
},
},
});

Expand Down Expand Up @@ -89,6 +94,11 @@ export default class ItemService {
ItemFolder: true,
ItemDocs: true,
ItemShortcut: true,
ItemStarred: {
where: {
userId: userId,
},
},
},
});

Expand Down Expand Up @@ -120,6 +130,11 @@ export default class ItemService {
ItemFolder: true,
ItemDocs: true,
ItemShortcut: true,
ItemStarred: {
where: {
userId: userId,
},
},
},
});

Expand All @@ -128,14 +143,16 @@ 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,
...ItemFolder,
...ItemDocs,
...ItemShortcut,
...strippedElement,
isStarred: ItemStarred.length > 0,
};
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/modules/item/starred/__test__/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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),
});
});

Expand Down
Loading

0 comments on commit 2200473

Please sign in to comment.