Skip to content

Commit

Permalink
[Fix] adds reviewed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tushar504 committed Nov 6, 2024
1 parent 7ffc28b commit e614623
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 35 deletions.
69 changes: 59 additions & 10 deletions src/presentation/http/router/noteList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ describe('GET /notes/:parentNoteId?page', () => {
expectedMessage: null,
expectedLength: 30,
pageNumber: 1,
isTeamMember: false,
isPublic: true,
},
/**
* Returns noteList with specified length (for last page)
Expand All @@ -168,6 +170,8 @@ describe('GET /notes/:parentNoteId?page', () => {
expectedMessage: null,
expectedLength: 19,
pageNumber: 2,
isTeamMember: false,
isPublic: true,
},
/**
* Returns noteList with no items if there are no notes for certain parentNote
Expand All @@ -179,6 +183,8 @@ describe('GET /notes/:parentNoteId?page', () => {
expectedMessage: null,
expectedLength: 0,
pageNumber: 3,
isTeamMember: false,
isPublic: true,
},
/**
* Returns 'querystring/page must be >= 1' message when page < 0
Expand All @@ -189,6 +195,8 @@ describe('GET /notes/:parentNoteId?page', () => {
expectedMessage: 'querystring/page must be >= 1',
expectedLength: 0,
pageNumber: -1,
isTeamMember: false,
isPublic: true,
},
/**
* Returns 'querystring/page must be <= 30' message when page is too large (maximum page numbrer is 30 by default)
Expand All @@ -199,6 +207,8 @@ describe('GET /notes/:parentNoteId?page', () => {
expectedMessage: 'querystring/page must be <= 30',
expectedLength: 0,
pageNumber: 31,
isTeamMember: false,
isPublic: true,
},
/**
* Returns 'unauthorized' message when user is not authorized
Expand All @@ -209,30 +219,69 @@ describe('GET /notes/:parentNoteId?page', () => {
expectedMessage: 'You must be authenticated to access this resource',
expectedLength: 0,
pageNumber: 1,
isTeamMember: false,
isPublic: true,
},
])('Get note list', async ({ isAuthorized, expectedStatusCode, expectedMessage, expectedLength, pageNumber }) => {
/**
* Returns noteList if user is in team
* User is authorized
*/
{
isAuthorized: true,
expectedStatusCode: 200,
expectedMessage: null,
expectedLength: 30,
pageNumber: 1,
isTeamMember: true,
isPublic: false,
},
/**
* Returns error message with no items if user is not in team
* User is authorized
*/
{
isAuthorized: true,
expectedStatusCode: 403,
expectedMessage: 'Permission denied',
expectedLength: 0,
pageNumber: 1,
isTeamMember: false,
isPublic: false,
},
])('Get note list', async ({ isAuthorized, expectedStatusCode, expectedMessage, expectedLength, pageNumber, isTeamMember, isPublic }) => {
const portionSize = 49;
let accessToken;

/** Insert creator and randomGuy */
/** Insert creator */
const creator = await global.db.insertUser();

const randomGuy = await global.db.insertUser();

if (isAuthorized) {
accessToken = global.auth(randomGuy.id);
}

/** Insert Note */
const parentNote = await global.db.insertNote({
creatorId: creator.id,
});

await global.db.insertNoteSetting({
const noteSetting = await global.db.insertNoteSetting({
noteId: parentNote.id,
cover: 'DZnvqi63.png',
isPublic: true,
isPublic: isPublic,
});

const randomGuy = await global.db.insertUser();

if (isAuthorized) {
accessToken = global.auth(randomGuy.id);
}

if (isTeamMember) {
await global.api?.fakeRequest({
method: 'POST',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: `/join/${noteSetting.invitationHash}`,
});
}

for (let i = 0; i < portionSize; i++) {
const note = await global.db.insertNote({
creatorId: creator.id,
Expand Down
7 changes: 2 additions & 5 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,8 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
description: 'Query notelist',
properties: {
items: {
id: { type: 'string' },
content: { type: 'string' },
createdAt: { type: 'string' },
creatorId: { type: 'string' },
updatedAt: { type: 'string' },
type: 'array',
items: { $ref: 'NoteSchema#' },
},
},
},
Expand Down
47 changes: 27 additions & 20 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default class NoteSequelizeStorage {
this.relationsModel = model;

this.model.hasMany(this.relationsModel, {
foreignKey: 'parentId',
foreignKey: 'noteId',
as: 'noteRelations',
});
}
Expand Down Expand Up @@ -373,27 +373,34 @@ export default class NoteSequelizeStorage {
throw new Error('Note settings model not initialized');
}

const childNotes = await this.relationsModel.findAll({
where: { parentId },
attributes: ['noteId'],
});

const noteIds = childNotes.map(relation => relation.noteId);

const reply = await this.model.findAll({
where: {
id: {
[Op.in]: noteIds,
include: [
{
model: this.relationsModel,
as: 'noteRelations',
where: {
parentId,
},
duplicating: false,
attributes: [],
},
},
include: [{
model: this.settingsModel,
as: 'noteSettings',
attributes: ['cover'],
duplicating: false,
}],
offset,
limit,
{
model: this.settingsModel,
as: 'noteSettings',
attributes: ['cover'],
duplicating: false,
},
],
order: [[
{
model: this.relationsModel,
as: 'noteRelations',
},
'id',
'DESC',
]],
offset: offset,
limit: limit,
});

return reply.map((note) => {
Expand Down

0 comments on commit e614623

Please sign in to comment.