Skip to content

Commit

Permalink
src/affiliate: Improve semantics
Browse files Browse the repository at this point in the history
CreateAffiliateDonationDto: Removed validator decorators from affiliateId, personId, billingName and billigEmail, as those fields shouldn't be part of the request body itself.
createAffiliateDonation(): Use chain conditioning to find whether affiliate exists or not.
  • Loading branch information
sashko9807 committed Oct 30, 2023
1 parent 52d67ee commit af8568a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
10 changes: 5 additions & 5 deletions apps/api/src/affiliate/affiliate.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { KeycloakTokenParsed, isAdmin } from '../auth/keycloak'
import { AuthenticatedUser, Public } from 'nest-keycloak-connect'
import { AffiliateService } from './affiliate.service'
import { AffiliateStatusUpdateDto } from './dto/affiliate-status-update.dto'
import { CreateAffiliateDonation } from './dto/create-affiliate-donation.dto'
import { CreateAffiliateDonationDto } from './dto/create-affiliate-donation.dto'
import { DonationsService } from '../donations/donations.service'
import { shouldAllowStatusChange } from '../donations/helpers/donation-status-updates'
import { affiliateCodeGenerator } from './utils/affiliateCodeGenerator'
Expand Down Expand Up @@ -80,15 +80,15 @@ export class AffiliateController {
@Public()
async createAffiliateDonation(
@Param('affiliateCode') affiliateCode: string,
@Body() donation: CreateAffiliateDonation,
@Body() donation: CreateAffiliateDonationDto,
) {
const affiliate = await this.affiliateService.findOneByCode(affiliateCode)
if (!affiliate || !affiliate.company || !affiliate.company.person)
throw new NotFoundException('Affiliate not found')
const affiliateDonationDto: CreateAffiliateDonation = {
if (!affiliate?.company?.person) throw new NotFoundException('Affiliate not found')
const affiliateDonationDto: CreateAffiliateDonationDto = {
...donation,
affiliateId: affiliate.id,
personId: donation.isAnonymous ? null : affiliate.company.person.id,
billingName: affiliate.company.person.firstName + ' ' + affiliate.company.person.lastName,
billingEmail: affiliate.company.person.email,
toEntity: donation.toEntity,
}
Expand Down
21 changes: 4 additions & 17 deletions apps/api/src/affiliate/dto/create-affiliate-donation.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@ import { Equals, IsBoolean, IsEnum, IsNumber, IsOptional, IsString, IsUUID } fro
import { randomUUID } from 'crypto'
import { DonationMetadataDto } from '../../donations/dto/donation-metadata.dto'

export class CreateAffiliateDonation {
export class CreateAffiliateDonationDto {
@ApiProperty()
@Expose()
@IsUUID()
@IsString()
campaignId: string

@ApiProperty()
@Expose()
@IsUUID()
@IsString()
@IsOptional()
affiliateId: string | null = null
affiliateId: string

@IsUUID()
@IsString()
@IsOptional()
personId: string | null = null
personId: string | null

@IsString()
extPaymentIntentId: string = 'pi_' + randomUUID()
Expand All @@ -45,13 +37,8 @@ export class CreateAffiliateDonation {
@IsNumber()
amount: number

@ApiProperty()
@Expose()
@IsString()
billingName: string
billingName: string | null

@IsString()
@IsOptional()
billingEmail: string | null

@ApiProperty()
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { donationWithPerson, DonationWithPerson } from './queries/donation.valid
import { CreateStripePaymentDto } from './dto/create-stripe-payment.dto'
import { ImportStatus } from '../bank-transactions-file/dto/bank-transactions-import-status.dto'
import { DonationQueryDto } from '../common/dto/donation-query-dto'
import { CreateAffiliateDonation } from '../affiliate/dto/create-affiliate-donation.dto'
import { CreateAffiliateDonationDto } from '../affiliate/dto/create-affiliate-donation.dto'

@Injectable()
export class DonationsService {
Expand Down Expand Up @@ -321,7 +321,7 @@ export class DonationsService {
return result
}

async createAffiliateDonation(donationDto: CreateAffiliateDonation) {
async createAffiliateDonation(donationDto: CreateAffiliateDonationDto) {
const vault = await this.vaultService.findByCampaignId(donationDto.campaignId)

if (!vault) throw new NotFoundException('Vault not found')
Expand Down

0 comments on commit af8568a

Please sign in to comment.