Skip to content

Commit

Permalink
src/affiliate: Add new endpoint to get affiliate's data by userid
Browse files Browse the repository at this point in the history
  • Loading branch information
sashko9807 committed Nov 4, 2023
1 parent 82eb4a4 commit c143e6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
16 changes: 14 additions & 2 deletions apps/api/src/affiliate/affiliate.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ export class AffiliateController {
private readonly campaignService: CampaignService,
) {}

@Get('data')
async findAffiliateByUserId(@AuthenticatedUser() user: KeycloakTokenParsed) {
const affiliate = await this.affiliateService.getAffiliateDataByKeycloakId(user.sub)
return affiliate
}

@Get(':affiliateCode')
@Public()
async affiliateSummary(@Param('affiliateCode') affilliateCode: string) {
return await this.affiliateService.getAffiliateSummaryByCode(affilliateCode)
const affiliate = await this.affiliateService.getAffiliateSummaryByCode(affilliateCode)
if (!affiliate) throw new NotFoundException('Affiliate not found')
return affiliate
}

@Post('join')
Expand Down Expand Up @@ -135,8 +143,12 @@ export class AffiliateController {
@Param('affiliateCode') affiliateCode: string,
@Param('donationId') donationId: string,
) {
console.log(`called`)
const donation = await this.donationService.getAffiliateDonationById(donationId, affiliateCode)
if (!donation) throw new NotFoundException('Donation with this id is not found')
if (!donation) {
console.log(`this is not found`)
throw new NotFoundException('Donation with this id is not found')
}

if (!shouldAllowStatusChange(donation.status, 'cancelled'))
throw new BadRequestException("Donation status can't be updated")
Expand Down
18 changes: 17 additions & 1 deletion apps/api/src/affiliate/affiliate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,25 @@ export class AffiliateService {
return await this.prismaService.affiliate.findUnique({ where: { id } })
}

async findAffiliateByKecloakId(keycloakId: string) {
async findAffiliateByKeycloakId(keycloakId: string) {
return await this.prismaService.affiliate.count({
where: { company: { person: { keycloakId } } },
})
}

async getAffiliateDataByKeycloakId(keycloakId: string) {
return await this.prismaService.affiliate.findFirst({
where: { company: { person: { keycloakId } } },
include: {
donations: {
where: { status: DonationStatus.guaranteed },
include: {
targetVault: { select: { campaign: { select: { title: true, slug: true } } } },
affiliate: { select: { company: { select: { companyName: true } } } },
metadata: { select: { name: true } },
},
},
},
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class BankTransactionsController {
const isDev = appEnv === 'development' || appEnv === 'staging'
if (!isDev) throw new ForbiddenException('Endpoint available only for testing enviroments')

const affiliate = await this.affiliateService.findAffiliateByKecloakId(user.sub)
const affiliate = await this.affiliateService.findAffiliateByKeycloakId(user.sub)
if (!isAdmin(user) && !affiliate)
throw new ForbiddenException('Must be either an admin or active affiliate')

Expand Down

0 comments on commit c143e6b

Please sign in to comment.