-
Notifications
You must be signed in to change notification settings - Fork 0
Pm 1345 trolley trust module #71
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- CreateEnum | ||
CREATE TYPE "verification_status" AS ENUM ('ACTIVE', 'INACTIVE'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "user_identity_verification_associations" ( | ||
"id" UUID NOT NULL DEFAULT uuid_generate_v4(), | ||
"user_id" VARCHAR(80) NOT NULL, | ||
"verification_id" TEXT NOT NULL, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"date_filed" TIMESTAMP(6) WITHOUT TIME ZONE NOT NULL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kkartunov do we really want this to be without timezone? |
||
"verification_status" "verification_status" NOT NULL, | ||
|
||
CONSTRAINT "user_identity_verification_associations_pkey" PRIMARY KEY ("id") | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { verification_status } from '@prisma/client'; | ||
import { PrismaService } from 'src/shared/global/prisma.service'; | ||
|
||
@Injectable() | ||
export class IdentityVerificationRepository { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
/** | ||
* Checks if the user has completed their identity verification by checking the identity verification associations | ||
* | ||
* @param userId - The unique identifier of the user. | ||
* @returns A promise that resolves to `true` if the user has at least one active identity verification association, otherwise `false`. | ||
*/ | ||
async completedIdentityVerification(userId: string): Promise<boolean> { | ||
const count = | ||
await this.prisma.user_identity_verification_associations.count({ | ||
where: { | ||
user_id: userId, | ||
verification_status: verification_status.ACTIVE, | ||
}, | ||
}); | ||
|
||
return count > 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,12 +23,19 @@ import { ResponseDto, ResponseStatusType } from 'src/dto/api-response.dto'; | |
import { SearchWinningResult, WinningRequestDto } from 'src/dto/winning.dto'; | ||
import { UserInfo } from 'src/dto/user.type'; | ||
import { UserWinningRequestDto } from './dto/user.dto'; | ||
import { PaymentsService } from 'src/shared/payments'; | ||
import { Logger } from 'src/shared/global'; | ||
|
||
@ApiTags('UserWinning') | ||
@Controller('/user') | ||
@ApiBearerAuth() | ||
export class UserController { | ||
constructor(private readonly winningsRepo: WinningsRepository) {} | ||
private readonly logger = new Logger(UserController.name); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constructor( | ||
private readonly winningsRepo: WinningsRepository, | ||
private readonly paymentsService: PaymentsService, | ||
) {} | ||
|
||
@Post('/winnings') | ||
@Roles(Role.User) | ||
|
@@ -59,6 +66,20 @@ export class UserController { | |
throw new ForbiddenException('insufficient permissions'); | ||
} | ||
|
||
try { | ||
await this.paymentsService.reconcileUserPayments(user.id); | ||
} catch (e) { | ||
this.logger.error('Error reconciling user payments', e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
error: { | ||
code: HttpStatus.INTERNAL_SERVER_ERROR, | ||
message: 'Failed to reconcile user payments.', | ||
}, | ||
status: ResponseStatusType.ERROR, | ||
} as ResponseDto<SearchWinningResult>; | ||
} | ||
|
||
const result = await this.winningsRepo.searchWinnings( | ||
body as WinningRequestDto, | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { WebhookEvent } from '../../webhooks.decorators'; | ||
import { PrismaService } from 'src/shared/global/prisma.service'; | ||
import { PaymentsService } from 'src/shared/payments'; | ||
import { Logger } from 'src/shared/global'; | ||
import { | ||
RecipientVerificationStatusUpdateEventData, | ||
RecipientVerificationWebhookEvent, | ||
} from './recipient-verification.types'; | ||
import { Prisma, verification_status } from '@prisma/client'; | ||
|
||
@Injectable() | ||
export class RecipientVerificationHandler { | ||
private readonly logger = new Logger(RecipientVerificationHandler.name); | ||
|
||
constructor( | ||
private readonly prisma: PrismaService, | ||
private readonly paymentsService: PaymentsService, | ||
) {} | ||
|
||
@WebhookEvent(RecipientVerificationWebhookEvent.statusUpdated) | ||
async handleStatusUpdated( | ||
payload: RecipientVerificationStatusUpdateEventData, | ||
): Promise<void> { | ||
const recipient = await this.prisma.trolley_recipient.findFirst({ | ||
where: { trolley_id: payload.recipientId }, | ||
}); | ||
|
||
if (!recipient) { | ||
this.logger.error( | ||
`Recipient with trolley_id ${payload.recipientId} not found.`, | ||
); | ||
throw new Error( | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`Recipient with trolley_id ${payload.recipientId} not found.`, | ||
); | ||
} | ||
|
||
const userIDV = | ||
await this.prisma.user_identity_verification_associations.findFirst({ | ||
where: { | ||
user_id: recipient.user_id, | ||
}, | ||
}); | ||
|
||
const verificationData: Prisma.user_identity_verification_associationsCreateInput = | ||
{ | ||
user_id: recipient.user_id, | ||
verification_id: payload.id, | ||
date_filed: payload.submittedAt ?? new Date(), | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
verification_status: | ||
payload.status === 'approved' | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? verification_status.ACTIVE | ||
: verification_status.INACTIVE, | ||
}; | ||
|
||
if (userIDV) { | ||
await this.prisma.user_identity_verification_associations.update({ | ||
where: { | ||
id: userIDV.id, | ||
}, | ||
data: { ...verificationData }, | ||
}); | ||
} else { | ||
await this.prisma.user_identity_verification_associations.create({ | ||
data: { ...verificationData }, | ||
}); | ||
} | ||
|
||
await this.paymentsService.reconcileUserPayments(recipient.user_id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export enum RecipientVerificationWebhookEvent { | ||
statusUpdated = 'recipientVerification.status_updated', | ||
} | ||
|
||
export interface RecipientVerificationStatusUpdateEventData { | ||
type: string; | ||
recipientId: string; | ||
status: 'pending' | 'approved' | 'rejected'; | ||
createdAt: string; | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
updatedAt: string; | ||
submittedAt: string | null; | ||
decisionAt: string | null; | ||
id: string; | ||
reasonType: string | null; | ||
verifiedData: { | ||
channel: 'sms' | 'email'; | ||
phone: string; | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
phoneExtension: string | null; | ||
country: string; | ||
}; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.