Skip to content

Commit

Permalink
beneficiery without mail (#555)
Browse files Browse the repository at this point in the history
* added: env variable for CAMPAIGN_ADMIN_MAIL in deployment config

* added default build command for vscode

* person email is now nullable

* fixed compiler errors after person.email become nullable

* formatting

* make email optional for createPersonDto
  • Loading branch information
quantum-grit authored Sep 29, 2023
1 parent 8f3c143 commit deca0d3
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 37 deletions.
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "build",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"label": "npm: build",
"detail": "yarn build"
}
]
}
22 changes: 12 additions & 10 deletions apps/api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ export class AuthService {
await this.marketingNotificationsService.provider.addContactsToList({
contacts: [
{
email: person.email,
first_name: person?.firstName || '',
last_name: person?.lastName || '',
email: registerDto.email,
first_name: registerDto.firstName,
last_name: registerDto.lastName,
},
],
list_ids: [mainList],
Expand Down Expand Up @@ -327,13 +327,15 @@ export class AuthService {

async sendMailForPasswordChange(forgotPasswordDto: ForgottenPasswordEmailDto) {
const stage = this.config.get<string>('APP_ENV') === 'development' ? 'APP_URL_LOCAL' : 'APP_URL'
const user = await this.prismaService.person.findFirst({
const person = await this.prismaService.person.findFirst({
where: { email: forgotPasswordDto.email },
})
if (!user) {

if (!person || !person.email) {
throw new NotFoundException('Invalid email')
}
const payload = { username: user.email, sub: user.keycloakId }

const payload = { username: person.email, sub: person.keycloakId }
const jtwSecret = process.env.JWT_SECRET_KEY
const access_token = this.jwtService.sign(payload, {
secret: jtwSecret,
Expand All @@ -342,12 +344,12 @@ export class AuthService {
const appUrl = this.config.get<string>(stage)
const link = `${appUrl}/change-password?token=${access_token}`
const profile = {
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
email: person.email,
firstName: person.firstName,
lastName: person.lastName,
link: link,
}
const userEmail = { to: [user.email] }
const userEmail = { to: [person.email] }
const mail = new ForgottenPasswordMailDto(profile)
await this.sendEmail.sendFromTemplate(mail, userEmail, {
//Allow users to receive the mail, regardles of unsubscribes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class CreatePersonDto {
firstName: string
lastName: string
email: string
email?: string
phone?: string
company?: string
newsletter?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Person {
id: string
firstName: string
lastName: string
email: string
email: string | null
phone: string | null
company: string | null
createdAt: Date
Expand Down
16 changes: 2 additions & 14 deletions apps/api/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Res,
Inject,
forwardRef,
NotFoundException,
} from '@nestjs/common'
import { ApiQuery, ApiTags } from '@nestjs/swagger'
import { DonationStatus } from '@prisma/client'
Expand Down Expand Up @@ -108,9 +107,7 @@ export class DonationsController {

@Get('user-donations')
async userDonations(@AuthenticatedUser() user: KeycloakTokenParsed) {
const person = await this.personService.findOneByKeycloakId(user.sub)
if (!person) throw new NotFoundException('User was not found')
return await this.donationsService.getDonationsByUser(user.sub, person.email)
return await this.donationsService.getDonationsByUser(user.sub, user.email)
}

@Get('money')
Expand Down Expand Up @@ -178,16 +175,7 @@ export class DonationsController {

@Get('user/:id')
async userDonationById(@Param('id') id: string, @AuthenticatedUser() user: KeycloakTokenParsed) {
const person = await this.personService.findOneByKeycloakId(user.sub)
if (!person) throw new NotFoundException('User was not found')
const donation = await this.donationsService.getUserDonationById(id, user.sub, person.email)
return {
...donation,
person: {
firstName: person.firstName,
lastName: person.lastName,
},
}
return await this.donationsService.getUserDonationById(id, user.sub, user.email)
}

@Post('payment-intent')
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export class DonationsService {
async getUserDonationById(
id: string,
keycloakId: string,
email: string,
email?: string,
): Promise<(Donation & { person: Person | null }) | null> {
return await this.prisma.donation.findFirst({
where: {
Expand Down Expand Up @@ -564,7 +564,7 @@ export class DonationsService {

const status = updatePaymentDto.status || currentDonation.status
let donorId = currentDonation.personId
let billingEmail = ''
let billingEmail: string | null = ''
if (
(updatePaymentDto.targetPersonId &&
currentDonation.personId !== updatePaymentDto.targetPersonId) ||
Expand Down Expand Up @@ -636,7 +636,7 @@ export class DonationsService {
}
}

async getDonationsByUser(keycloakId: string, email: string) {
async getDonationsByUser(keycloakId: string, email?: string) {
const donations = await this.prisma.donation.findMany({
where: {
OR: [{ billingEmail: email }, { person: { keycloakId } }],
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class MarketingNotificationsService {
return new BadRequestException('Failed to get user')
}

if (!userInfo) return new BadRequestException('User not found')
if (!userInfo || !userInfo.email) return new BadRequestException('User not found')

// Already unsubscribed
if (!userInfo.newsletter) return { email: userInfo.email, subscribed: false }
Expand Down Expand Up @@ -306,7 +306,7 @@ export class MarketingNotificationsService {
return new BadRequestException('Failed to get user')
}

if (!userInfo) return new BadRequestException('User not found')
if (!userInfo || !userInfo.email) return new BadRequestException('User not found')

// Already subscribed
if (userInfo.newsletter) return { email: userInfo.email, subscribed: true }
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/person/dto/create-person.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class CreatePersonDto {
@ApiProperty()
@Expose()
@IsEmail()
email: string
@IsOptional()
email?: string

@ApiProperty()
@Expose()
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/person/person.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import client from '@sendgrid/client'
import mailClient from '@sendgrid/client'
import { Prisma } from '@prisma/client'
import { PrismaService } from '../prisma/prisma.service'
import { CreatePersonDto } from './dto/create-person.dto'
Expand All @@ -16,7 +16,7 @@ export class PersonService {
const apiKey = config.get<string>('sendgrid.apiKey')

if (apiKey && this.contactsUrl) {
client.setApiKey(apiKey)
mailClient.setApiKey(apiKey)
} else {
this.enabled = false
Logger.warn('no apiKey or contactsUrl for sendgrid, will not add user to the contact list')
Expand Down Expand Up @@ -138,7 +138,7 @@ export class PersonService {
}

try {
await client.request({
await mailClient.request({
url: this.contactsUrl,
method: 'PUT',
body: data,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "people" ALTER COLUMN "email" DROP NOT NULL;
2 changes: 1 addition & 1 deletion podkrepi.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Table people {
id String [pk]
firstName String [not null]
lastName String [not null]
email String [unique, not null]
email String [unique]
phone String
company String
createdAt DateTime [default: `now()`, not null]
Expand Down
2 changes: 1 addition & 1 deletion schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ model Person {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
firstName String @map("first_name") @db.VarChar(100)
lastName String @map("last_name") @db.VarChar(100)
email String @unique @db.Citext
email String? @unique @db.Citext
phone String? @db.VarChar(50)
company String? @db.VarChar(50)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
Expand Down

0 comments on commit deca0d3

Please sign in to comment.