Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/campaign: Cache list endpoint #580

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/api/src/campaign/campaign.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import { ApiTags } from '@nestjs/swagger'
import { CampaignNewsService } from '../campaign-news/campaign-news.service'
import { CampaignSubscribeDto } from './dto/campaign-subscribe.dto'

import { CacheInterceptor, CacheKey } from '@nestjs/cache-manager'
import { UseInterceptors } from '@nestjs/common'
import { CAMPAIGN_LIST } from '../common/cacheKeys'

@ApiTags('campaign')
@Controller('campaign')
export class CampaignController {
Expand All @@ -41,6 +45,8 @@ export class CampaignController {
) {}

@Get('list')
@UseInterceptors(CacheInterceptor)
@CacheKey(CAMPAIGN_LIST)
@Public()
async getData() {
return this.campaignService.listCampaigns()
Expand Down
9 changes: 8 additions & 1 deletion apps/api/src/campaign/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ import {
NotificationService,
donationNotificationSelect,
} from '../sockets/notifications/notification.service'
import { DonationMetadata } from '../donations/dontation-metadata.interface'

import { Expense } from '@prisma/client'
import { SendGridParams } from '../notifications/providers/notifications.sendgrid.types'
import * as NotificationData from '../notifications/notification-data.json'
import { ConfigService } from '@nestjs/config'
import { DateTime } from 'luxon'
import { CampaignSubscribeDto } from './dto/campaign-subscribe.dto'
import { MarketingNotificationsService } from '../notifications/notifications.service'
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Cache } from 'cache-manager'
import { CAMPAIGN_LIST } from '../common/cacheKeys'

@Injectable()
export class CampaignService {
Expand All @@ -60,6 +63,7 @@ export class CampaignService {
@Inject(forwardRef(() => MarketingNotificationsService))
private marketingNotificationsService: MarketingNotificationsService,
private readonly config: ConfigService,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}

async listCampaigns(): Promise<CampaignListItem[]> {
Expand Down Expand Up @@ -643,6 +647,7 @@ export class CampaignService {
...updatedDonation,
person: updatedDonation.person,
})
await this.cacheManager.del(CAMPAIGN_LIST)
} else if (
donation.status === DonationStatus.succeeded &&
newDonationStatus === DonationStatus.refund
Expand All @@ -656,6 +661,7 @@ export class CampaignService {
...updatedDonation,
person: updatedDonation.person,
})
await this.cacheManager.del(CAMPAIGN_LIST)
}
return updatedDonation
} catch (error) {
Expand Down Expand Up @@ -713,6 +719,7 @@ export class CampaignService {
if (newDonationStatus === DonationStatus.succeeded) {
await this.vaultService.incrementVaultAmount(donation.targetVaultId, donation.amount, tx)
this.notificationService.sendNotification('successfulDonation', donation)
await this.cacheManager.del(CAMPAIGN_LIST)
}

return donation
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/common/cacheKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* List of cache key definitions for manual manipulation(e.g. deleting, caching) etc..
*/

export const CAMPAIGN_LIST = 'campaignList'
9 changes: 8 additions & 1 deletion apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Stripe from 'stripe'
import { ConfigService } from '@nestjs/config'
import { InjectStripeClient } from '@golevelup/nestjs-stripe'
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common'
import { BadRequestException, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common'
import {
Campaign,
Donation,
Expand All @@ -28,6 +28,9 @@ 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 { CreateAffiliateDonationDto } from '../affiliate/dto/create-affiliate-donation.dto'
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Cache } from 'cache-manager'
import { CAMPAIGN_LIST } from '../common/cacheKeys'

@Injectable()
export class DonationsService {
Expand All @@ -38,6 +41,7 @@ export class DonationsService {
private prisma: PrismaService,
private vaultService: VaultService,
private exportService: ExportService,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}
async listPrices(type?: Stripe.PriceListParams.Type, active?: boolean): Promise<Stripe.Price[]> {
const listResponse = await this.stripeClient.prices.list({ active, type, limit: 100 }).then(
Expand Down Expand Up @@ -340,6 +344,7 @@ export class DonationsService {
},
})
}
await this.cacheManager.del(CAMPAIGN_LIST)
return donation
}

Expand Down Expand Up @@ -588,6 +593,7 @@ export class DonationsService {
donationDto.amount,
tx,
)
await this.cacheManager.del(CAMPAIGN_LIST)
return ImportStatus.SUCCESS
}

Expand All @@ -609,6 +615,7 @@ export class DonationsService {
data: { status: DonationStatus.succeeded, updatedAt: donationDto.updatedAt },
}),
])
await this.cacheManager.del(CAMPAIGN_LIST)
})
}

Expand Down
Loading