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-news: Extend API response data to accomodate design changes #626

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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: 0 additions & 6 deletions apps/api/src/campaign-news/campaign-news.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ export class CampaignNewsController {
return await this.campaignNewsService.listAllArticles()
}

@Get(':slug')
@Public()
async findOne(@Param('slug') slug: string) {
return await this.campaignNewsService.findArticleBySlug(slug)
}

@Get('byId/:id')
@Public()
async findById(@Param('id') id: string) {
Expand Down
25 changes: 22 additions & 3 deletions apps/api/src/campaign-news/campaign-news.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger, BadRequestException, NotFoundException } from '@nes
import { CreateCampaignNewsDto } from './dto/create-campaign-news.dto'
import { PrismaService } from '../prisma/prisma.service'
import { UpdateCampaignNewsDto } from './dto/update-campaign-news.dto'
import { CampaignNewsState } from '@prisma/client'
import { CampaignFileRole, CampaignNewsState } from '@prisma/client'
import { CampaignNews } from '../domain/generated/campaignNews/entities'
import { SendGridParams } from '../notifications/providers/notifications.sendgrid.types'
import { DateTime } from 'luxon'
Expand Down Expand Up @@ -160,12 +160,14 @@ export class CampaignNewsService {
slug: true,
author: true,
publishedAt: true,
description: true,
newsFiles: true,
campaign: {
select: {
title: true,
state: true,
slug: true,
campaignFiles: { where: { role: CampaignFileRole.campaignListPhoto } },
campaignType: { select: { category: true } },
},
},
},
Expand Down Expand Up @@ -248,7 +250,24 @@ export class CampaignNewsService {

async findArticleBySlug(slug: string) {
return await this.prisma.campaignNews
.findFirst({ where: { slug: slug }, include: { newsFiles: true } })
.findFirst({
where: { slug: slug },
include: {
newsFiles: true,
campaign: {
select: {
id: true,
title: true,
targetAmount: true,
campaignType: { select: { category: true } },
state: true,
allowDonationOnComplete: true,
slug: true,
campaignFiles: { where: { role: CampaignFileRole.campaignListPhoto } },
},
},
},
})
.catch((error) => Logger.warn(error))
}

Expand Down
13 changes: 13 additions & 0 deletions apps/api/src/campaign/campaign.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ export class CampaignController {
return this.campaignNewsService.listPublishedNewsWithPagination(page)
}

@Get('news/:articleSlug')
@Public()
async viewSingleCampaignArticle(@Param('articleSlug') articleSlug: string) {
const article = await this.campaignNewsService.findArticleBySlug(articleSlug)
if (!article) throw new NotFoundException(`Article with slug ${articleSlug} not found`)
const campaignSummary = await this.campaignService.getCampaignSums([article.campaignId])
article.campaign['summary'] = this.campaignService.getVaultAndDonationSummaries(
article.campaign.id,
campaignSummary,
)
return article
}

@Get(':slug')
@Public()
async viewBySlug(@Param('slug') slug: string): Promise<{ campaign: Campaign | null }> {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/campaign/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ export class CampaignService {
})
}

private getVaultAndDonationSummaries(campaignId: string, campaignSums: CampaignSummaryDto[]) {
getVaultAndDonationSummaries(campaignId: string, campaignSums: CampaignSummaryDto[]) {
const csum = campaignSums.find((e) => e.id === campaignId)
return {
reachedAmount: csum?.reachedAmount || 0,
Expand Down
Loading