Skip to content

Commit

Permalink
adding song delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ivorfarshore committed Nov 20, 2024
1 parent f95ee5f commit 14fbf93
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/modules/song/song.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
HttpCode,
Param,
Expand Down Expand Up @@ -109,4 +110,18 @@ export class SongController {
});
response.data.pipe(res);
}

@Delete(':id')
@Auth(Role.Artist)
@UseFilters(new HttpExceptionFilter())
@CommonApiResponse(SongDto)
async deleteSong(@Req() request: AuthRequest, @Param('id') id: string) {
return handle(
await this.songService.deleteSong(
id,
request.user.id,
request.user.roles,
),
);
}
}
38 changes: 38 additions & 0 deletions src/modules/song/song.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,42 @@ export class SongService {
return new ServerError<string>(`Can't get song media`);
}
}

async deleteSong(
id: string,
userId: string,
roles: Role[],
): Promise<ServiceResult<SongDto>> {
try {
if (!roles.includes(Role.Artist)) {
return new BadRequest<SongDto>(
`You don't have permission for this operation!`,
);
}

const song = await this.songRepository.findOne({
where: { id },
relations: ['user'],
});

if (!song) {
return new NotFound<SongDto>(`Song not found!`);
}

if (song.user.id !== userId) {
return new Forbidden<SongDto>(`You can only delete your own songs!`);
}

// Delete the Stripe price if it exists
if (song.price_id) {
await this.stripeService.archivePrice(song.price_id);
}

await this.songRepository.remove(song);
return new ServiceResult<SongDto>(SongDto.fromEntity(song));
} catch (error) {
this.logger.error('SongService - deleteSong', error);
return new ServerError<SongDto>(`Can't delete song`);
}
}
}
11 changes: 11 additions & 0 deletions src/modules/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,15 @@ export class StripeService {
return new ServerError<boolean>(`Checkout session completed error`);
}
}

async archivePrice(priceId: string): Promise<void> {
try {
await this.stripe.prices.update(priceId, {
active: false,
});
} catch (error) {
this.logger.error('StripeService - archivePrice', error);
throw new Error('Failed to archive Stripe price');
}
}
}

0 comments on commit 14fbf93

Please sign in to comment.