|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { UserInfo } from 'src/dto/user.dto'; |
| 3 | +import { TrolleyService as Trolley } from 'src/shared/global/trolley.service'; |
| 4 | +import { PrismaService } from 'src/shared/global/prisma.service'; |
| 5 | +import { BASIC_MEMBER_FIELDS } from 'src/shared/topcoder'; |
| 6 | +import { TopcoderMembersService } from 'src/shared/topcoder/members.service'; |
| 7 | + |
| 8 | +@Injectable() |
| 9 | +export class TrolleyService { |
| 10 | + constructor( |
| 11 | + private readonly trolley: Trolley, |
| 12 | + private readonly prisma: PrismaService, |
| 13 | + private readonly tcMembersService: TopcoderMembersService, |
| 14 | + ) {} |
| 15 | + |
| 16 | + /** |
| 17 | + * Retrieves the Trolley payment method record from the database. |
| 18 | + * Throws an error if the record does not exist. |
| 19 | + */ |
| 20 | + private async getTrolleyPaymentMethod() { |
| 21 | + const method = await this.prisma.payment_method.findUnique({ |
| 22 | + where: { payment_method_type: 'Trolley' }, |
| 23 | + }); |
| 24 | + |
| 25 | + if (!method) { |
| 26 | + throw new Error("DB record for payment method 'Trolley' not found!"); |
| 27 | + } |
| 28 | + |
| 29 | + return method; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Attempts to find an existing Trolley recipient by email. |
| 34 | + * If none exists, creates a new one using data fetched from member api |
| 35 | + * |
| 36 | + * @param user - Current user |
| 37 | + */ |
| 38 | + private async findOrCreateTrolleyRecipient(user: UserInfo) { |
| 39 | + const foundRecipient = await this.trolley.client.recipient.search( |
| 40 | + 1, |
| 41 | + 1, |
| 42 | + user.email, |
| 43 | + ); |
| 44 | + |
| 45 | + if (foundRecipient?.length === 1) { |
| 46 | + return foundRecipient[0]; |
| 47 | + } |
| 48 | + |
| 49 | + const userInfo = await this.tcMembersService.getMemberInfoByUserHandle( |
| 50 | + user.handle, |
| 51 | + { fields: BASIC_MEMBER_FIELDS }, |
| 52 | + ); |
| 53 | + const address = userInfo.addresses?.[0] ?? {}; |
| 54 | + |
| 55 | + const recipientPayload = { |
| 56 | + type: 'individual' as const, |
| 57 | + referenceId: user.id, |
| 58 | + firstName: userInfo.firstName, |
| 59 | + lastName: userInfo.lastName, |
| 60 | + email: user.email, |
| 61 | + address: { |
| 62 | + city: address.city, |
| 63 | + postalCode: address.zip, |
| 64 | + region: address.stateCode, |
| 65 | + street1: address.streetAddr1, |
| 66 | + street2: address.streetAddr2, |
| 67 | + }, |
| 68 | + }; |
| 69 | + |
| 70 | + return this.trolley.client.recipient.create(recipientPayload); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates and links a Trolley recipient with the user in the local DB. |
| 75 | + * Uses a transaction to ensure consistency between user payment method creation |
| 76 | + * and Trolley recipient linkage. |
| 77 | + * |
| 78 | + * @param user - Basic user info (e.g., ID, handle, email). |
| 79 | + * @returns Trolley recipient DB model tied to the user. |
| 80 | + */ |
| 81 | + private async createPayeeRecipient(user: UserInfo) { |
| 82 | + const recipient = await this.findOrCreateTrolleyRecipient(user); |
| 83 | + |
| 84 | + const paymentMethod = await this.getTrolleyPaymentMethod(); |
| 85 | + |
| 86 | + return this.prisma.$transaction(async (tx) => { |
| 87 | + let userPaymentMethod = await tx.user_payment_methods.findFirst({ |
| 88 | + where: { |
| 89 | + user_id: user.id, |
| 90 | + payment_method_id: paymentMethod.payment_method_id, |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + if (!userPaymentMethod) { |
| 95 | + userPaymentMethod = await tx.user_payment_methods.create({ |
| 96 | + data: { |
| 97 | + user_id: user.id, |
| 98 | + payment_method: { connect: paymentMethod }, |
| 99 | + }, |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + const updatedUserPaymentMethod = await tx.user_payment_methods.update({ |
| 104 | + where: { id: userPaymentMethod.id }, |
| 105 | + data: { |
| 106 | + trolley_payment_method: { |
| 107 | + create: { |
| 108 | + user_id: user.id, |
| 109 | + trolley_id: recipient.id, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + include: { |
| 114 | + trolley_payment_method: true, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + return updatedUserPaymentMethod.trolley_payment_method?.[0]; |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Fetches the Trolley recipient associated with the given user. |
| 124 | + * If none exists, creates and stores a new one. |
| 125 | + * |
| 126 | + * @param user - Basic user info |
| 127 | + * @returns Trolley recipient DB model |
| 128 | + */ |
| 129 | + async getPayeeRecipient(user: UserInfo) { |
| 130 | + const dbRecipient = await this.prisma.trolley_recipient.findUnique({ |
| 131 | + where: { user_id: user.id }, |
| 132 | + }); |
| 133 | + |
| 134 | + if (dbRecipient) { |
| 135 | + return dbRecipient; |
| 136 | + } |
| 137 | + |
| 138 | + return this.createPayeeRecipient(user); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Generates a portal URL for the user to access their Trolley dashboard. |
| 143 | + * |
| 144 | + * @param user - User information used to fetch Trolley recipient. |
| 145 | + * @returns A URL string to the Trolley user portal. |
| 146 | + */ |
| 147 | + async getPortalUrlForUser(user: UserInfo) { |
| 148 | + const recipient = await this.getPayeeRecipient(user); |
| 149 | + const link = this.trolley.getRecipientPortalUrl({ |
| 150 | + email: user.email, |
| 151 | + trolleyId: recipient.trolley_id, |
| 152 | + }); |
| 153 | + |
| 154 | + return { link, recipientId: recipient.trolley_id }; |
| 155 | + } |
| 156 | +} |
0 commit comments