refactor
This commit is contained in:
@@ -1,25 +1,29 @@
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
|
||||
|
||||
import type { UpdateTeamCommandDTO } from '../dto/TeamCommandAndQueryDTO';
|
||||
|
||||
type UpdateTeamErrorCode = 'INSUFFICIENT_PERMISSIONS' | 'TEAM_NOT_FOUND';
|
||||
|
||||
export class UpdateTeamUseCase {
|
||||
constructor(
|
||||
private readonly teamRepository: ITeamRepository,
|
||||
private readonly membershipRepository: ITeamMembershipRepository,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateTeamCommandDTO): Promise<void> {
|
||||
async execute(command: UpdateTeamCommandDTO): Promise<Result<void, ApplicationErrorCode<UpdateTeamErrorCode>>> {
|
||||
const { teamId, updates, updatedBy } = command;
|
||||
|
||||
const updaterMembership = await this.membershipRepository.getMembership(teamId, updatedBy);
|
||||
if (!updaterMembership || (updaterMembership.role !== 'owner' && updaterMembership.role !== 'manager')) {
|
||||
throw new Error('Only owners and managers can update team info');
|
||||
return Result.err({ code: 'INSUFFICIENT_PERMISSIONS' });
|
||||
}
|
||||
|
||||
const existing = await this.teamRepository.findById(teamId);
|
||||
if (!existing) {
|
||||
throw new Error('Team not found');
|
||||
return Result.err({ code: 'TEAM_NOT_FOUND' });
|
||||
}
|
||||
|
||||
const updated = existing.update({
|
||||
@@ -30,5 +34,7 @@ export class UpdateTeamUseCase {
|
||||
});
|
||||
|
||||
await this.teamRepository.update(updated);
|
||||
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user