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

fix: Incorrect customer/Stripe details stored in DB [DEV-4005] #543

Merged
merged 10 commits into from
Jun 19, 2024
2,364 changes: 1,216 additions & 1,148 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@cheqd/ts-proto": "^3.4.2",
"@cosmjs/amino": "^0.32.3",
"@cosmjs/encoding": "^0.32.3",
"@logto/express": "^2.3.9",
"@logto/express": "^2.3.11",
"@stablelib/ed25519": "^1.0.3",
"@veramo/core": "^5.6.0",
"@veramo/credential-ld": "^5.6.0",
Expand Down Expand Up @@ -92,25 +92,25 @@
"jsonwebtoken": "^9.0.2",
"jwt-decode": "^4.0.0",
"loglevel": "^1.9.1",
"multiformats": "^13.1.0",
"multiformats": "^13.1.1",
"node-cache": "^5.1.2",
"pg": "^8.11.5",
"pg": "^8.12.0",
"pg-connection-string": "^2.6.4",
"secp256k1": "^5.0.0",
"sqlite3": "^5.1.7",
"stripe": "^14.25.0",
"swagger-ui-dist": "5.10.5",
"swagger-ui-express": "^5.0.0",
"swagger-ui-express": "^5.0.1",
"typeorm": "^0.3.20",
"uint8arrays": "^5.1.0",
"uri-js": "^4.4.1"
},
"devDependencies": {
"@playwright/test": "^1.44.0",
"@playwright/test": "^1.44.1",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^11.1.0",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^10.0.4",
"@semantic-release/github": "^10.0.6",
"@semantic-release/npm": "^11.0.3",
"@semantic-release/release-notes-generator": "^13.0.0",
"@types/bcrypt": "^5.0.2",
Expand All @@ -123,12 +123,12 @@
"@types/helmet": "^4.0.0",
"@types/json-stringify-safe": "^5.0.3",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^20.12.12",
"@types/node": "^20.14.5",
"@types/secp256k1": "^4.0.6",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"@types/uuid": "^9.0.8",
"@types/validator": "^13.11.10",
"@types/validator": "^13.12.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"buffer": "6.0.3",
Expand All @@ -137,10 +137,10 @@
"eslint-config-prettier": "^9.1.0",
"eslint-config-typescript": "^3.0.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"prettier": "^3.3.2",
"semantic-release": "^23.1.1",
"swagger-jsdoc": "^6.2.8",
"ts-jest": "^29.1.2",
"ts-jest": "^29.1.5",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.5",
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/admin/organisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export class OrganisationController {
async update(request: Request, response: Response) {
const { name, email, description } = request.body;
try {
const customer = await CustomerService.instance.update(
response.locals.customer.customerId,
const customer = await CustomerService.instance.update({
customerId: response.locals.customer.customerId,
name,
email,
description
);
description,
});
const paymentAccount = await PaymentAccountService.instance.find({ customer: customer });

if (!customer || paymentAccount.length === 0) {
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ export class AccountController {
}
const logToUserId = request.body.user.id;
const logToUserEmail = request.body.user.primaryEmail;
const logToName = request.body.user.name || ''; // setting empty string as backup incase it was null.

const defaultRole = await RoleService.instance.getDefaultRole();
if (!defaultRole) {
return response.status(StatusCodes.BAD_REQUEST).json({
Expand All @@ -189,7 +191,9 @@ export class AccountController {
// Even if customer was created before for such user but the process was interruted somehow - we need to create it again
// Cause we don't know the state of the customer in this case
// 2.1.1. Create customer
customer = (await CustomerService.instance.create(logToUserEmail)) as CustomerEntity;
// I’m setting the "name" field to an empty string on the current CustomerEntity because it is non-nullable.
// we will populate the customer's "name" field using the response from the Stripe account creation in account-submitter.ts.
customer = (await CustomerService.instance.create(logToName, logToUserEmail)) as CustomerEntity;
if (!customer) {
return response.status(StatusCodes.BAD_REQUEST).json({
error: 'User is not found in database: Customer was not created',
Expand Down
11 changes: 4 additions & 7 deletions src/services/api/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as dotenv from 'dotenv';
import { PaymentAccountService } from './payment-account.js';
import { CheqdNetwork } from '@cheqd/sdk';
import { v4 as uuidv4 } from 'uuid';
import type { UpdateCustomerEntity } from '../../types/customer.js';
dotenv.config();

export class CustomerService {
Expand Down Expand Up @@ -42,13 +43,9 @@ export class CustomerService {
};
}

public async update(
customerId: string,
name?: string,
email?: string,
description?: string,
paymentProviderId?: string
) {
public async update(customer: UpdateCustomerEntity) {
const { customerId, name, email, description, paymentProviderId } = customer;

const existingCustomer = await this.customerRepository.findOneBy({ customerId });
if (!existingCustomer) {
throw new Error(`CustomerId not found`);
Expand Down
8 changes: 6 additions & 2 deletions src/services/track/admin/account-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ export class PortalAccountCreateSubmitter implements IObserver {
return;
}

// Update the CaaS customer with the new Stripe account
await CustomerService.instance.update(data.customerId, undefined, account.id);
// Update the CaaS customer with the new Stripe account. Note, we're populating the "name" field from stripe's response.
await CustomerService.instance.update({
customerId: data.customerId,
name: data.name,
paymentProviderId: account.id,
});
await this.notify({
message: EventTracker.compileBasicNotification(
`Stripe account created with name: ${data.name}.`,
Expand Down
3 changes: 3 additions & 0 deletions src/types/customer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CustomerEntity } from '../database/entities/customer.entity.js';
import type { UnsuccessfulQueryResponseBody } from './shared.js';

// Positive
Expand All @@ -12,6 +13,8 @@ export type QueryCustomerResponseBody = {
};
};

export type UpdateCustomerEntity = Partial<CustomerEntity> & Pick<CustomerEntity, 'customerId'>;

export type QueryIdTokenResponseBody = {
idToken: string;
};
Expand Down
Loading