diff --git a/src/domain/entities/team.ts b/src/domain/entities/team.ts index 82415efd..b2b7a7c5 100644 --- a/src/domain/entities/team.ts +++ b/src/domain/entities/team.ts @@ -1,4 +1,4 @@ -import type { NoteInternalId } from './note.js'; +import type { NoteInternalId, NotePublicId } from './note.js'; import type User from './user.js'; export enum MemberRole { @@ -39,6 +39,11 @@ export interface TeamMember { role: MemberRole; } +/** + * Team member public entity sends to user with public id of the note + */ +export type TeamMemberPublic = Omit & { noteId: NotePublicId }; + export type Team = TeamMember[]; export type TeamMemberCreationAttributes = Omit; diff --git a/src/domain/service/note.ts b/src/domain/service/note.ts index 323ce519..9a09405b 100644 --- a/src/domain/service/note.ts +++ b/src/domain/service/note.ts @@ -310,4 +310,16 @@ export default class NoteService { throw new DomainError('Incorrect tools passed'); } } + + /** + * Get note public id by it's internal id + * Used for making entities that use NoteInternalId public + * @param id - internal id of the note + * @returns note public id + */ + public async getNotePublicIdByInternal(id: NoteInternalId): Promise { + const note = await this.noteRepository.getNoteById(id); + + return note!.publicId; + } } diff --git a/src/domain/service/noteSettings.ts b/src/domain/service/noteSettings.ts index a0d03c40..322b46b4 100644 --- a/src/domain/service/noteSettings.ts +++ b/src/domain/service/noteSettings.ts @@ -3,7 +3,7 @@ import type { InvitationHash } from '@domain/entities/noteSettings.js'; import type NoteSettings from '@domain/entities/noteSettings.js'; import type NoteSettingsRepository from '@repository/noteSettings.repository.js'; import type TeamRepository from '@repository/team.repository.js'; -import type { Team, TeamMember, TeamMemberCreationAttributes } from '@domain/entities/team.js'; +import type { Team, TeamMember, TeamMemberPublic, TeamMemberCreationAttributes } from '@domain/entities/team.js'; import { MemberRole } from '@domain/entities/team.js'; import type User from '@domain/entities/user.js'; import { createInvitationHash } from '@infrastructure/utils/invitationHash.js'; @@ -38,7 +38,7 @@ export default class NoteSettingsService { * @param invitationHash - hash for joining to the team * @param userId - user to add */ - public async addUserToTeamByInvitationHash(invitationHash: InvitationHash, userId: User['id']): Promise { + public async addUserToTeamByInvitationHash(invitationHash: InvitationHash, userId: User['id']): Promise { const defaultUserRole = MemberRole.Read; const noteSettings = await this.noteSettingsRepository.getNoteSettingsByInvitationHash(invitationHash); @@ -58,11 +58,18 @@ export default class NoteSettingsService { throw new DomainError(`User already in team`); } - return await this.teamRepository.createTeamMembership({ + const teamMember = await this.teamRepository.createTeamMembership({ noteId: noteSettings.noteId, userId, role: defaultUserRole, }); + + return { + id: teamMember.id, + noteId: await this.shared.note.getNotePublicIdByInternal(teamMember.noteId), + userId: teamMember.userId, + role: teamMember.role, + }; } /** diff --git a/src/domain/service/shared/note.ts b/src/domain/service/shared/note.ts index 0c0acff4..eee962f2 100644 --- a/src/domain/service/shared/note.ts +++ b/src/domain/service/shared/note.ts @@ -1,4 +1,5 @@ import type { NoteInternalId } from '@domain/entities/note.js'; +import type { NotePublicId } from '@domain/entities/note.js'; /** * Which methods of Domain can be used by other domains @@ -11,4 +12,11 @@ export default interface NoteServiceSharedMethods { * @param noteId - id of the current note */ getParentNoteIdByNoteId(noteId: NoteInternalId): Promise; + + /** + * Get note public id by it's internal id + * Used for making entities that use NoteInternalId public + * @param id - internal id of the note + */ + getNotePublicIdByInternal(noteId: NoteInternalId): Promise; } diff --git a/src/presentation/http/router/join.ts b/src/presentation/http/router/join.ts index 3c51c336..8f34c293 100644 --- a/src/presentation/http/router/join.ts +++ b/src/presentation/http/router/join.ts @@ -1,6 +1,6 @@ import type { FastifyPluginCallback } from 'fastify'; import type NoteSettingsService from '@domain/service/noteSettings.js'; -import type { TeamMember } from '@domain/entities/team.js'; +import type { TeamMemberPublic } from '@domain/entities/team.js'; /** * Represents AI router options @@ -55,7 +55,7 @@ const JoinRouter: FastifyPluginCallback = (fastify, opts, don }, async (request, reply) => { const { hash } = request.params; const { userId } = request; - let result: TeamMember | null = null; + let result: TeamMemberPublic | null = null; try { result = await noteSettingsService.addUserToTeamByInvitationHash(hash, userId as number);