Skip to content

Commit

Permalink
update: first part of code review is done
Browse files Browse the repository at this point in the history
  • Loading branch information
dependentmadani committed Sep 4, 2024
1 parent 0551b9f commit a5adcc2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/domain/entities/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ export interface Note {
export type NoteCreationAttributes = Pick<Note, 'publicId' | 'content' | 'creatorId' | 'tools'>;

/**
* The return type for note parent structure
* Represents the content of a parent note.
*/
export type NoteParentStructure = {
export type NoteParentContent = {
/**
* Note public id
*/
Expand Down
4 changes: 2 additions & 2 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Note, NoteInternalId, NoteParentStructure, NotePublicId } from '@domain/entities/note.js';
import type { Note, NoteInternalId, NoteParentContent, NotePublicId } from '@domain/entities/note.js';
import type NoteRepository from '@repository/note.repository.js';
import type NoteVisitsRepository from '@repository/noteVisits.repository.js';
import { createPublicId } from '@infrastructure/utils/id.js';
Expand Down Expand Up @@ -457,7 +457,7 @@ export default class NoteService {
* @param userId - id of the user that is requesting the parent structure
* @returns - array of notes that are parent structure of the note
*/
public async getNoteParentStructure(noteId: NoteInternalId, userId: number): Promise<Array<NoteParentStructure>> {
public async getNoteParentStructure(noteId: NoteInternalId, userId: number): Promise<Array<NoteParentContent>> {
return await this.teamRepository.getAllNotesParents(noteId, userId);
}
}
4 changes: 4 additions & 0 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
*/
const canEdit = memberRole === MemberRole.Write;

const noteParentContent = await noteService.getNoteParentStructure(noteId, userId!);

console.log('noteParentContent', noteParentContent);

return reply.send({
note: notePublic,
parentNote: parentNote,
Expand Down
7 changes: 3 additions & 4 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from
import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js';
import type { NoteSettingsModel } from './noteSettings.js';
import type { NoteVisitsModel } from './noteVisits.js';
import { DomainError } from '@domain/entities/DomainError.js';
import type { NoteHistoryModel } from './noteHistory.js';

/* eslint-disable @typescript-eslint/naming-convention */
Expand Down Expand Up @@ -233,11 +232,11 @@ export default class NoteSequelizeStorage {
*/
public async getNoteListByUserId(userId: number, offset: number, limit: number): Promise<Note[]> {
if (this.visitsModel === null) {
throw new DomainError('NoteVisit model should be defined');
throw new Error('NoteStorage: NoteVisit model should be defined');
}

if (!this.settingsModel) {
throw new Error('Note settings model not initialized');
throw new Error('NoteStorage: Note settings model not initialized');
}

const reply = await this.model.findAll({
Expand Down Expand Up @@ -293,7 +292,7 @@ export default class NoteSequelizeStorage {
*/
public async getNoteByHostname(hostname: string): Promise<Note | null> {
if (!this.settingsModel) {
throw new Error('Note settings model not initialized');
throw new Error('NoteStorage: Note settings model not initialized');
}

/**
Expand Down
40 changes: 21 additions & 19 deletions src/repository/storage/postgres/orm/sequelize/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import type { Team, TeamMemberCreationAttributes, TeamMember } from '@domain/ent
import { UserModel } from './user.js';
import { MemberRole } from '@domain/entities/team.js';
import type User from '@domain/entities/user.js';
import type { NoteInternalId, NoteParentStructure } from '@domain/entities/note.js';
import { DomainError } from '@domain/entities/DomainError.js';
import type { NoteInternalId, NoteParentContent } from '@domain/entities/note.js';
import type { NoteRelationsModel } from './noteRelations.js';

/**
Expand All @@ -33,6 +32,11 @@ export class TeamsModel extends Model<InferAttributes<TeamsModel>, InferCreation
* Team member role, show what user can do with note
*/
public declare role: MemberRole;

/**
* Note relation content
*/
public declare notes?: NoteModel | null;
}

/**
Expand Down Expand Up @@ -202,7 +206,7 @@ export default class TeamsSequelizeStorage {
*/
public async getTeamMembersWithUserInfoByNoteId(noteId: NoteInternalId): Promise<Team> {
if (!this.userModel) {
throw new DomainError('User model not initialized');
throw new Error('TeamStorage: User model not defined');
}

return await this.model.findAll({
Expand All @@ -219,15 +223,15 @@ export default class TeamsSequelizeStorage {

/**
* Get note parent structure
* @param noteId : the ID of the note.
* @param userId : The ID of the user.
* @param noteId - the ID of the note.
* @param userId - the ID of the user.
*/
public async getAllNoteParents(noteId: NoteInternalId, userId: number): Promise<NoteParentStructure[]> {
public async getAllNoteParents(noteId: NoteInternalId, userId: number): Promise<NoteParentContent[]> {
if (!this.noteModel || !this.noteRelationModel) {
throw new DomainError('Note model or Note relation model not initialized');
throw new Error(`${this.noteModel !== null ? 'TeamStorage: Note relation model is not defined' : 'TeamStorage: Note model is not defined'}`);
}

const parentNotes: NoteParentStructure[] = [];
const parentNotes: NoteParentContent[] = [];
let currentNoteId: NoteInternalId | null = noteId;

while (currentNoteId != null) {
Expand All @@ -237,20 +241,18 @@ export default class TeamsSequelizeStorage {
noteId: currentNoteId,
userId,
},
include: {
model: this.noteModel,
as: this.noteModel.tableName,
required: true,
},
});

if (teamMember) {
// If the user does not have access, add the note to the array
const note = await this.noteModel.findOne({
where: { id: currentNoteId },
if (teamMember && teamMember.notes !== undefined && teamMember.notes !== null) {
parentNotes.push({
noteId: teamMember.notes.publicId,
content: teamMember.notes.content,
});

if (note) {
parentNotes.push({
noteId: note.publicId,
content: note.content,
});
}
}

// Retrieve the parent note
Expand Down
4 changes: 2 additions & 2 deletions src/repository/team.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type TeamStorage from '@repository/storage/team.storage.js';
import type { Team, TeamMember, TeamMemberCreationAttributes, MemberRole } from '@domain/entities/team.js';
import type { NoteInternalId, NoteParentStructure } from '@domain/entities/note.js';
import type { NoteInternalId, NoteParentContent } from '@domain/entities/note.js';
import type User from '@domain/entities/user.js';

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ export default class TeamRepository {
* @param userId : user id to check access
* @returns an array of note parents objects containing public id and content
*/
public async getAllNotesParents(noteId: NoteInternalId, userId: number): Promise<NoteParentStructure[]> {
public async getAllNotesParents(noteId: NoteInternalId, userId: number): Promise<NoteParentContent[]> {
return await this.storage.getAllNoteParents(noteId, userId);
}

Expand Down

0 comments on commit a5adcc2

Please sign in to comment.