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

Send admin notification for campaign news upload #546

Merged
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ PLATFORM_IBAN=
IMPORT_TRX_TASK_INTERVAL_MINUTES=60
CHECK_IRIS_CONSENT_TASK_HOUR=10
[email protected]
CAMPAIGN_ADMIN_MAIL=

## Cache ##
##############
Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ IMPORT_TRX_TASK_INTERVAL_MINUTES=60
#which hour of the day to run the check for consent
CHECK_IRIS_CONSENT_TASK_HOUR=10
[email protected]
CAMPAIGN_ADMIN_MAIL=responsible for campaign management

## Cache ##
##############
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/assets/templates/campaign-news-draft.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"subject": "Новина по Кампания Е Качена за Одобрение"
}
69 changes: 69 additions & 0 deletions apps/api/src/assets/templates/campaign-news-draft.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<mjml>
<mj-body background-color="#ffffff" font-size="13px" width="90%">
<mj-section
background-color="#009FE3"
vertical-align="top"
padding-bottom="0px"
padding-top="0">
<mj-column vertical-align="top" width="100%">
<mj-text
align="center"
color="#ffffff"
font-size="45px"
font-weight="bold"
font-family="open Sans Helvetica, Arial, sans-serif"
padding-left="25px"
padding-right="25px"
padding-top="50px">
{{campaignNewsTitle}}
</mj-text>
</mj-column>
</mj-section>
<mj-section background-color="#009fe3" padding-bottom="20px">
<mj-column vertical-align="middle" width="100%">
<mj-text
align="left"
color="#ffffff"
font-size="22px"
font-family="open Sans Helvetica, Arial, sans-serif"
padding-left="25px"
padding-right="25px">
<br /><br />
</mj-text>
<mj-text
align="center"
color="#ffffff"
font-size="24px"
font-family="open Sans Helvetica, Arial, sans-serif"
padding-left="25px"
padding-right="25px">
Има качена новина по кампания <a href="{{campaignLink}}">{{campaignName}}</a>, която
изчаква одобрение от администратор!
</mj-text>

<mj-button
background-color="#feeb35"
font-family="Helvetica, Arial, sans-serif"
font-size="17px"
border-radius="30px"
color="#000000"
padding="15px 30px"
href="{{newsLink}}"
target="_blank">
Към Новината
</mj-button>

<mj-text
align="left"
color="#ffffff"
font-size="15px"
font-family="open Sans Helvetica, Arial, sans-serif"
padding-left="25px"
padding-right="25px">
Поздрави, <br />
Екипът на Подкрепи.бг
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
3 changes: 2 additions & 1 deletion apps/api/src/campaign-news/campaign-news.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { PrismaService } from '../prisma/prisma.service'
import { PersonModule } from '../person/person.module'
import { MarketingNotificationsModule } from '../notifications/notifications.module'
import { ConfigService } from '@nestjs/config'
import { EmailService } from '../email/email.service'

@Module({
imports: [PersonModule, MarketingNotificationsModule],
controllers: [CampaignNewsController],
providers: [CampaignNewsService, PrismaService, ConfigService],
providers: [CampaignNewsService, PrismaService, ConfigService, EmailService],
exports: [CampaignNewsService],
})
export class CampaignNewsModule {}
44 changes: 43 additions & 1 deletion apps/api/src/campaign-news/campaign-news.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import { SendGridParams } from '../notifications/providers/notifications.sendgri
import { DateTime } from 'luxon'
import { ConfigService } from '@nestjs/config'
import { MarketingNotificationsService } from '../notifications/notifications.service'
import { CampaignNewsDraftEmailDto } from '../email/template.interface'
import { EmailService } from '../email/email.service'

@Injectable()
export class CampaignNewsService {
constructor(
private prisma: PrismaService,
private readonly config: ConfigService,
private sendEmail: EmailService,
private readonly marketingNotificationsService: MarketingNotificationsService,
) {}
private RECORDS_PER_PAGE = 4
Expand All @@ -24,8 +27,14 @@ export class CampaignNewsService {
try {
const campaignNews = await this.prisma.campaignNews.create({ data: campaignNewsDto })
if (campaignNews.state === 'published' && notify)
// USER Notification
//Don't await --> send to background
this.sendArticleNotification(campaignNews).catch((e) => console.log(e))
this.sendArticleNotification(campaignNews).catch((e) => Logger.warn(e))

// ADMIN Notification
//Don't await --> send to background
this.notifyAdminsForNewsUpload(campaignNews).catch((e) => Logger.warn(e))

return campaignNews
} catch (error) {
const message = 'Creating article about campaign failed'
Expand All @@ -34,6 +43,39 @@ export class CampaignNewsService {
}
}

async notifyAdminsForNewsUpload(news: CampaignNews) {
const campaign = await this.prisma.campaign.findFirst({
where: { id: news.campaignId },
})

if (!campaign) return

// Build the links
const stage = this.config.get<string>('APP_ENV') === 'development' ? 'APP_URL_LOCAL' : 'APP_URL'
const appUrl = this.config.get<string>(stage)
const newsLink = `${appUrl}/campaigns/${campaign.slug}/news/admin-panel`
const campaignLink = `${appUrl}/campaigns/${campaign.slug}`
const campaignAdminEmail = this.config.get<string>('mail.campaignAdminEmail', '')

if (!campaignAdminEmail) return

// Prepare Email data
const recepient = { to: [campaignAdminEmail] }

const mail = new CampaignNewsDraftEmailDto({
campaignName: campaign.title,
campaignNewsTitle: news.title,
campaignLink,
newsLink,
})

// Send Notification
await this.sendEmail.sendFromTemplate(mail, recepient, {
//Allow users to receive the mail, regardles of unsubscribes
bypassUnsubscribeManagement: { enable: true },
})
}

async sendArticleNotification(news: CampaignNews) {
const template = await this.prisma.marketingTemplates.findFirst({
where: {
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export default () => ({
banksEndPoint: process.env.IRIS_API_URL + '/banks?country=bulgaria',
ibansEndPoint: process.env.IRIS_API_URL + '/ibans',
transactionsEndPoint: process.env.IRIS_API_URL + '/transactions',
},
mail: {
billingAdminEmail: process.env.BILLING_ADMIN_MAIL,
campaignAdminEmail: process.env.CAMPAIGN_ADMIN_MAIL,
},
tasks: {
import_transactions: { interval: process.env.IMPORT_TRX_TASK_INTERVAL_MINUTES },
Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/email/template.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum TemplateType {
unrecognizedDonation = 'unrecognized-donation',
expiringIrisConsent = 'expiring-iris-consent',
confirmConsent = 'confirm-notifications-consent',
campaignNewsDraft = 'campaign-news-draft',
}
export type TemplateTypeKeys = keyof typeof TemplateType
export type TemplateTypeValues = typeof TemplateType[TemplateTypeKeys]
Expand Down Expand Up @@ -80,3 +81,12 @@ export class ConfirmConsentEmailDto extends EmailTemplate<{
}> {
name = TemplateType.confirmConsent
}

export class CampaignNewsDraftEmailDto extends EmailTemplate<{
campaignLink: string
campaignName: string
newsLink: string
campaignNewsTitle: string
}> {
name = TemplateType.campaignNewsDraft
}
4 changes: 2 additions & 2 deletions apps/api/src/tasks/bank-import/import-transactions.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class IrisTasks {
this.bankBIC = this.config.get<string>('iris.bankBIC', '')
this.IBAN = this.config.get<string>('iris.platformIBAN', '')
this.apiUrl = this.config.get<string>('iris.apiUrl', '')
this.billingAdminEmail = this.config.get<string>('iris.billingAdminEmail', '')
this.billingAdminEmail = this.config.get<string>('mail.billingAdminEmail', '')

this.checkForRequiredVariables()
}
Expand Down Expand Up @@ -324,7 +324,7 @@ export class IrisTasks {
transactionAmount.currency = Currency.BGN
} else {
// mark as unrecognized
matchedRef = null;
matchedRef = null
}
}

Expand Down