Skip to content

Commit

Permalink
fix: Add customerId into operation entity [DEV-3732] (#507)
Browse files Browse the repository at this point in the history
* Add customerId into operation entity

* Add customer to operation tracking

* Fix typo in filename

---------

Co-authored-by: Ankur Banerjee <[email protected]>
  • Loading branch information
Andrew Nikitin and ankurdotb authored Apr 15, 2024
1 parent 33058d9 commit b580646
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/database/entities/operation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { categoryEnum } from './../types/enum.js';
import { CoinEntity } from './coin.entity.js';

import * as dotenv from 'dotenv';
import { CustomerEntity } from './customer.entity.js';
dotenv.config();

@Entity('operation')
Expand Down Expand Up @@ -63,19 +64,25 @@ export class OperationEntity {
@JoinColumn({ name: 'defaultFee' })
defaultFee!: CoinEntity;

@ManyToOne(() => CustomerEntity, (customer) => customer.customerId, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'customerId' })
customer!: CustomerEntity;

constructor(
operationId: string,
category: string,
operationName: string,
defaultFee: CoinEntity,
deprecated: boolean,
successful: boolean
successful: boolean,
customer: CustomerEntity
) {
this.operationId = operationId;
this.category = category;
this.operationName = operationName;
this.defaultFee = defaultFee;
this.deprecated = deprecated;
this.successful = successful;
this.customer = customer;
}
}
31 changes: 31 additions & 0 deletions src/database/migrations/AlterOperationTableAddCustomer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Table, TableColumn, TableForeignKey, type MigrationInterface, type QueryRunner } from 'typeorm';

export class AlterOperationTableAddCustomer1695740345990 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const table_name = 'operation';
const table = (await queryRunner.getTable(table_name)) as Table;

await queryRunner.addColumn(
table_name,
new TableColumn({
name: 'customerId',
type: 'uuid',
isNullable: true,
})
);

await queryRunner.createForeignKey(
table,
new TableForeignKey({
columnNames: ['customerId'],
referencedColumnNames: ['customerId'],
referencedTableName: 'customer',
onDelete: 'CASCADE',
})
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
throw new Error('illegal_operation: cannot roll back initial migration');
}
}
3 changes: 3 additions & 0 deletions src/database/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { AlterOperationTable1695740345978 } from '../migrations/AlterOperationTa
import { AlterPaymentTable1695740345979 } from '../migrations/AlterPaymentTable.js';
import { CreateCoinTable1695740345977 } from '../migrations/CreateCoinTable.js';
import { CoinEntity } from '../entities/coin.entity.js';
import { AlterOperationTableAddCustomer1695740345990 } from '../migrations/AlterOperationTableAddCustomer.js';
import { AlterCustomerTable1695740346000 } from '../migrations/AlterCustomerTable.js';
import { AlterOperationTable1695740346001 } from '../migrations/AlterOperationTableNewCategory.js';
import { SubscriptionEntity } from '../entities/subscription.entity.js';
Expand Down Expand Up @@ -100,6 +101,8 @@ export class Postgres implements AbstractDatabase {
AlterOperationTable1695740345978,
// Change payment table structure
AlterPaymentTable1695740345979,
// Add Customer relation to Operation table
AlterOperationTableAddCustomer1695740345990,
// Add paymentProviderId to customer table
AlterCustomerTable1695740346000,
// Add new category
Expand Down
11 changes: 7 additions & 4 deletions src/services/api/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as dotenv from 'dotenv';
import { OperationEntity } from '../../database/entities/operation.entity.js';
import { v4 } from 'uuid';
import type { CoinEntity } from '../../database/entities/coin.entity.js';
import type { CustomerEntity } from '../../database/entities/customer.entity.js';
dotenv.config();

export class OperationService {
Expand All @@ -22,7 +23,8 @@ export class OperationService {
operationName: string,
defaultFee: CoinEntity,
deprecated = false,
successful = true
successful = true,
customer: CustomerEntity
): Promise<OperationEntity> {
if (!category) {
throw new Error('Operation category is not specified');
Expand All @@ -37,7 +39,8 @@ export class OperationService {
operationName,
defaultFee,
deprecated,
successful
successful,
customer
);
const operation = (await this.operationRepository.insert(operationEntity)).identifiers[0];
if (!operation) throw new Error(`Cannot create a new operation`);
Expand Down Expand Up @@ -79,14 +82,14 @@ export class OperationService {
public async get(operationId: string) {
return await this.operationRepository.findOne({
where: { operationId },
relations: ['coin'],
relations: ['coin', 'customer'],
});
}

public async find(where: Record<string, unknown>) {
return await this.operationRepository.find({
where: where,
relations: ['coin'],
relations: ['coin', 'customer'],
});
}
}
3 changes: 2 additions & 1 deletion src/services/track/operation-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export class DBOperationSubscriber extends BaseOperationObserver implements IObs
trackOperation.name,
defaultFee,
false,
trackOperation.successful
trackOperation.successful,
trackOperation.customer
);

if (!operationEntity) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ITrackOperation {
// data of the operation, e.g. did, resource, credentialStatus
data: TrackData;
// customer who initiated the operation (like organistation)
customer?: CustomerEntity;
customer: CustomerEntity;
// user who initiated the operation
user?: UserEntity;
// was operation successful?
Expand Down

0 comments on commit b580646

Please sign in to comment.