Rize only allows you to connect one plaid_external account, so you must modify it in the Plaid dashboard so that only one can be selected by default, since the first account returned by Plaid will be taken if you use the third_party_connection field as true.
This would be the flow to request a link token from Plaid:
This is the flow to make a direct connection with the BaaS:
If a direct connection to the BaaS is not made, you must make a request by sending the id of the account you want to connect:
This section will have the steps to integrate Plaid with Rize, but if you want to know more about Rize, you can see this section:
First, we will import some files in aggregators/aggregators.module.ts
We will import the BaaS module because we will use the Rize service to send our account data in Plaid, and it will be created in the BaaS.
We will add the integration model in TypeOrmModule.forFeature because we will query the database when the user did the last integration, since Rize doesn't allow to connect a new account before 30 days after deleting an account.
Now, in the Plaid service's constructor, we must create our variables that will have the instance for the Rize service and the connection to the integration repository.
We will modify the file aggregators/plaid.service.ts:
This method is automatically executed if, when using the aggregators/banking-connection endpoint, the third_party_connection property is passed as true.
In the function called mapAccount, you must change the following code:
Map Account
Before:
public async mapAccount(account, user: User): Promise<CustomAccountDto> {// here you must check which accounts you have connected to// the baas to return the account id and status. return { baas_account_uid:null, account_id:account.account_id, name:account.name, baas_status:null, subtype:account.subtype, type:account.type, mask:account.mask, balances:account.balances, };}
After:
In this function, a SyntheticAccount variable is declared, which will be used to check if the account it receives as a parameter is connected to the BaaS.
If the account is connected to the BaaS, the id assigned by the BaaS and the account status will be returned, because when creating the account, the BaaS assigns a started status to the account, and then the BaaS contacts Plaid to get the associated account information. If everything was successful, the account goes from started to active, but if a failure occurs, it goes from started to failed, and another request must be made again to try to connect the account.
This method is used to generate the processor token according to the service with which Plaid is to connect. You can see all the services allowed in this link.
In Rize, after deleting an account with plaid_external type, you have to wait 30 days to create a new one. Therefore, a method was created to verify the number of days elapsed. This verification is done only if the user has previously deleted an account.
public async verifyLastIntegration(user: User): Promise<void> { const integration =awaitthis.integrationRepository.findOne({ where: { userId:user.id, service:IntegrationServices.PLAID, }, withDeleted:true, order: { createdAt:'DESC', }, });if (integration && integration.deletedAt) {constdays=dayjs().diff(dayjs(integration.deletedAt),'days');if (days <=30) {thrownewUnprocessableEntityException('You must wait 30 days to be able to reconnect an account', ); } }}