Skip to content

Commit

Permalink
refactor: handle duplicate transaction labels
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 5, 2025
1 parent 6db48fc commit e1e294c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
3 changes: 3 additions & 0 deletions lib/db/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ReverseRoutingHint from './models/ReverseRoutingHint';
import ReverseSwap from './models/ReverseSwap';
import Swap from './models/Swap';
import TransactionLabel from './models/TransactionLabel';
import TransactionLabelRepository from './repositories/TransactionLabelRepository';

// To make sure that PostgreSQL types are parsed correctly
types.setTypeParser(types.builtins.INT8, parseInt);
Expand Down Expand Up @@ -145,6 +146,8 @@ class Database {
PendingLockupTransaction.load(Database.sequelize);
PendingEthereumTransaction.load(Database.sequelize);
Rebroadcast.load(Database.sequelize);

TransactionLabelRepository.setLogger(this.logger);
};
}

Expand Down
38 changes: 32 additions & 6 deletions lib/db/repositories/TransactionLabelRepository.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import Logger from '../../Logger';
import { SwapType, swapTypeToPrettyString } from '../../consts/Enums';
import { AnySwap } from '../../consts/Types';
import TransactionLabel from '../models/TransactionLabel';

class TransactionLabelRepository {
public static addLabel = async (id: string, symbol: string, label: string) =>
TransactionLabel.create({
id,
label,
symbol,
});
public static logger?: Logger;

public static setLogger = (logger: Logger) => {
this.logger = logger;
};

public static addLabel = async (
id: string,
symbol: string,
label: string,
) => {
try {
return await TransactionLabel.create({
id,
label,
symbol,
});
} catch (error) {
if ((error as any).name === 'SequelizeUniqueConstraintError') {
const existingLabel = await TransactionLabel.findOne({ where: { id } });
if (existingLabel) {
this.logger?.warn(
`Updating existing label for ${id} from "${existingLabel.label}" to "${label}"`,
);
return await existingLabel.update({ label });
}
}

throw error;
}
};

public static getLabel = (id: string) =>
TransactionLabel.findOne({
Expand Down
39 changes: 29 additions & 10 deletions test/integration/db/repositories/TransactionLabelRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,37 @@ describe('TransactionLabelRepository', () => {
await database.close();
});

test('should add labels', async () => {
const txId = 'id';
const symbol = 'BTC';
const label = 'important info';
describe('addLabel', () => {
test('should add labels', async () => {
const txId = 'id';
const symbol = 'BTC';
const label = 'important info';

await TransactionLabelRepository.addLabel(txId, symbol, label);
await TransactionLabelRepository.addLabel(txId, symbol, label);

const entry = await TransactionLabelRepository.getLabel(txId);
const entry = await TransactionLabelRepository.getLabel(txId);

expect(entry).not.toBeNull();
expect(entry!.id).toEqual(txId);
expect(entry!.symbol).toEqual(symbol);
expect(entry!.label).toEqual(label);
expect(entry).not.toBeNull();
expect(entry!.id).toEqual(txId);
expect(entry!.symbol).toEqual(symbol);
expect(entry!.label).toEqual(label);
});

test('should update existing labels', async () => {
const txId = 'id';
const symbol = 'BTC';
const originalLabel = 'original label';
const newLabel = 'updated label';

await TransactionLabelRepository.addLabel(txId, symbol, originalLabel);
await TransactionLabelRepository.addLabel(txId, symbol, newLabel);

const entry = await TransactionLabelRepository.getLabel(txId);

expect(entry).not.toBeNull();
expect(entry!.id).toEqual(txId);
expect(entry!.symbol).toEqual(symbol);
expect(entry!.label).toEqual(newLabel);
});
});
});

0 comments on commit e1e294c

Please sign in to comment.