Skip to content

Commit

Permalink
#31 - Added changes according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders164a committed Oct 17, 2023
1 parent bf902cf commit 80bcdf3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 126 deletions.
100 changes: 48 additions & 52 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe('GET /api/item/:parentId', () => {
});
});

describe('GET /api/item/:parentId/single', () => {
describe('GET /api/item/:id/single', () => {
let userService: UserService;
let folderService: FolderService;
let authService: AuthService;
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('GET /api/item/:parentId/single', () => {
});
});

it('Should return status 200 and item from id', async () => {
it('Should return status 200 and folder item from id', async () => {
const { accessToken } = await authService.createTokens(user.id);

const folder = await folderService.createFolder({
Expand All @@ -269,21 +269,6 @@ describe('GET /api/item/:parentId/single', () => {
parentId: null,
});

const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
parentId: null,
blobUrl: 'https://example.com/test1.txt',
});

const docs = await docsService.createDocs({
name: 'Docser',
ownerId: user.id,
parentId: null,
text: 'Docs text here!',
});

const responseFolder = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + folder.id + '/single',
Expand All @@ -292,74 +277,85 @@ describe('GET /api/item/:parentId/single', () => {
},
});

const responseBlob = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + blob.id + '/single',
headers: {
authorization: 'Bearer ' + accessToken,
},
});

const responseDocs = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + docs.id + '/single',
headers: {
authorization: 'Bearer ' + accessToken,
},
});

expect(responseFolder.statusCode).toBe(200);
expect(responseFolder.json()).toEqual({
expect(responseFolder.json()[0]).toEqual({
id: expect.any(Number),
name: 'Folder1',
color: '#123456',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.folder',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
ItemBlob: null,
ItemFolder: {
id: expect.any(Number),
color: '#123456',
isStarred: false,
});
});

it('Should return status 200 and blob item from id', async () => {
const { accessToken } = await authService.createTokens(user.id);

const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
parentId: null,
blobUrl: 'https://example.com/test1.txt',
});

const responseBlob = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + blob.id + '/single',
headers: {
authorization: 'Bearer ' + accessToken,
},
ItemDocs: null,
});

expect(responseBlob.statusCode).toBe(200);
expect(responseBlob.json()).toEqual({
expect(responseBlob.json()[0]).toEqual({
id: expect.any(Number),
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),
ItemBlob: {
id: expect.any(Number),
blobUrl: 'https://example.com/test1.txt',
isStarred: false,
});
});

it('Should return status 200 and docs item from id', async () => {
const { accessToken } = await authService.createTokens(user.id);

const docs = await docsService.createDocs({
name: 'Docser',
ownerId: user.id,
parentId: null,
text: 'Docs text here!',
});

const responseDocs = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + docs.id + '/single',
headers: {
authorization: 'Bearer ' + accessToken,
},
ItemFolder: null,
ItemDocs: null,
});

expect(responseDocs.statusCode).toBe(200);
expect(responseDocs.json()).toEqual({
expect(responseDocs.json()[0]).toEqual({
id: expect.any(Number),
name: 'Docser',
text: 'Docs text here!',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.docs',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
ItemBlob: null,
ItemFolder: null,
ItemDocs: {
id: expect.any(Number),
text: 'Docs text here!',
},
isStarred: false,
});
});

Expand Down
6 changes: 6 additions & 0 deletions src/modules/item/__test__/item.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,10 @@ describe('ItemService', () => {
await expect(itemService.getItemByIdWithSharingsAndOwner(1234)).rejects.toThrow();
});
});

describe('getItemByIdWithInclude()', () => {
it("should throw error, when item doesn't exist", async () => {
await expect(itemService.getItemByIdWithInclude(1234, user.id)).rejects.toThrow();
});
});
});
2 changes: 1 addition & 1 deletion src/modules/item/item.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class ItemController {
return reply.unauthorized();
}

const item = await this.itemService.getItemByIdWithFolderAndDocsAndBlob(id);
const item = await this.itemService.getItemByIdWithInclude(id, request.user.sub);

return reply.code(200).send(item);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/item/item.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async (fastify: FastifyInstance) => {
schema: {
tags: ['Item'],
response: {
200: { $ref: 'itemFolderDocsBlobResponseSchema' },
200: { $ref: 'itemsResponseSchema' },
},
security: [
{
Expand Down
71 changes: 1 addition & 70 deletions src/modules/item/item.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,77 +226,8 @@ const itemSharingsResponseSchema = {
},
} as const;

const itemFolderDocsBlobResponseSchema = {
$id: 'itemFolderDocsBlobResponseSchema',
type: 'object',
properties: {
id: {
type: 'number',
},
name: {
type: 'string',
},
mimeType: {
type: 'string',
},
ownerId: {
type: 'number',
},
parentId: {
type: ['number', 'null'],
},
ItemFolder: {
type: ['object', 'null'],
properties: {
id: {
type: 'number',
},
color: {
type: 'string',
},
},
},
ItemBlob: {
type: ['object', 'null'],
properties: {
id: {
type: 'number',
},
blobUrl: {
type: 'string',
},
},
},
ItemDocs: {
type: ['object', 'null'],
properties: {
id: {
type: 'number',
},
text: {
type: 'string',
},
},
},
deletedAt: {
type: ['string', 'null'],
},
createdAt: {
type: 'string',
},
updatedAt: {
type: 'string',
},
},
} as const;

export type itemSharingsInput = FromSchema<typeof itemSharingsSchema>;
export type itemReadInput = FromSchema<typeof itemReadSchema>;
export type ReadInput = FromSchema<typeof readItemsSchema>;

export const itemSchemas = [
readItemsSchema,
itemsResponseSchema,
itemSharingsResponseSchema,
itemFolderDocsBlobResponseSchema,
];
export const itemSchemas = [readItemsSchema, itemsResponseSchema, itemSharingsResponseSchema];
14 changes: 12 additions & 2 deletions src/modules/item/item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export default class ItemService {
return item;
}

public async getItemByIdWithFolderAndDocsAndBlob(id: number) {
public async getItemByIdWithInclude(id: number, userId: number): Promise<ItemWithProperties[]> {
const item = await prisma.item.findUnique({
where: {
id,
Expand All @@ -200,10 +200,20 @@ export default class ItemService {
ItemBlob: true,
ItemFolder: true,
ItemDocs: true,
ItemShortcut: true,
ItemStarred: {
where: {
userId: userId,
},
},
},
});

return item;
if (!item) {
throw new Error('item.notFound');
}

return this.formatItems([item]);
}

private formatItems(items: ItemPrismaProperties[]): ItemWithProperties[] {
Expand Down

0 comments on commit 80bcdf3

Please sign in to comment.