Skip to content

Commit

Permalink
implemented public team member
Browse files Browse the repository at this point in the history
  • Loading branch information
e11sy committed Jul 4, 2024
1 parent 17be90d commit 49bb773
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/domain/entities/team.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<TeamMember, 'noteId'> & { noteId: NotePublicId };

export type Team = TeamMember[];

export type TeamMemberCreationAttributes = Omit<TeamMember, 'id'>;
12 changes: 12 additions & 0 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotePublicId> {
const note = await this.noteRepository.getNoteById(id);

return note!.publicId;
}
}
13 changes: 10 additions & 3 deletions src/domain/service/noteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<TeamMember | null> {
public async addUserToTeamByInvitationHash(invitationHash: InvitationHash, userId: User['id']): Promise<TeamMemberPublic | null> {
const defaultUserRole = MemberRole.Read;
const noteSettings = await this.noteSettingsRepository.getNoteSettingsByInvitationHash(invitationHash);

Expand All @@ -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,
};
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/domain/service/shared/note.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,4 +12,11 @@ export default interface NoteServiceSharedMethods {
* @param noteId - id of the current note
*/
getParentNoteIdByNoteId(noteId: NoteInternalId): Promise<NoteInternalId | null>;

/**
* 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<NotePublicId>;
}
4 changes: 2 additions & 2 deletions src/presentation/http/router/join.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -55,7 +55,7 @@ const JoinRouter: FastifyPluginCallback<JoinRouterOptions> = (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);
Expand Down

0 comments on commit 49bb773

Please sign in to comment.