-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49bf13e
commit 64fba5a
Showing
15 changed files
with
1,047 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { DataTypes, Model, Sequelize } from 'sequelize'; | ||
|
||
type ExtraFeeType = { | ||
swapId: string; | ||
id: string; | ||
fee: number; | ||
percentage: number; | ||
}; | ||
|
||
class ExtraFee extends Model implements ExtraFeeType { | ||
public swapId!: string; | ||
public id!: string; | ||
public fee!: number; | ||
public percentage!: number; | ||
|
||
public createdAt!: Date; | ||
public updatedAt!: Date; | ||
|
||
public static load = (sequelize: Sequelize): void => { | ||
ExtraFee.init( | ||
{ | ||
swapId: { | ||
type: new DataTypes.STRING(255), | ||
primaryKey: true, | ||
allowNull: false, | ||
}, | ||
id: { | ||
type: new DataTypes.STRING(255), | ||
allowNull: false, | ||
}, | ||
fee: { | ||
type: new DataTypes.BIGINT(), | ||
allowNull: true, | ||
}, | ||
percentage: { | ||
type: new DataTypes.DECIMAL(), | ||
allowNull: false, | ||
}, | ||
}, | ||
{ | ||
sequelize, | ||
tableName: 'extra_fees', | ||
indexes: [ | ||
{ | ||
unique: false, | ||
fields: ['id'], | ||
}, | ||
], | ||
}, | ||
); | ||
}; | ||
} | ||
|
||
export default ExtraFee; | ||
export { ExtraFeeType }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { QueryTypes } from 'sequelize'; | ||
import { SwapUpdateEvent } from '../../consts/Enums'; | ||
import { getNestedObject } from '../../data/Utils'; | ||
import Database from '../Database'; | ||
import ExtraFee, { ExtraFeeType } from '../models/ExtraFee'; | ||
|
||
type GroupedByYearMonth<T> = Record<string, Record<string, T>>; | ||
|
||
class ExtraFeeRepository { | ||
private static readonly statsQuery = ` | ||
WITH successful AS (SELECT id, status, referral, "createdAt" | ||
FROm swaps | ||
WHERE status = ? | ||
UNION ALL | ||
SELECT id, status, referral, "createdAt" | ||
FROM "reverseSwaps" | ||
WHERE status = ? | ||
UNION ALL | ||
SELECT id, status, referral, "createdAt" | ||
FROM "chainSwaps" | ||
WHERE status = ?), | ||
successful_extra AS (SELECT e.id AS id, e.fee AS fee, e."createdAt" | ||
FROM successful s | ||
RIGHT JOIN extra_fees e on s.id = e."swapId" | ||
WHERE referral = ?) | ||
SELECT EXTRACT(YEAR FROM "createdAt") AS year, EXTRACT(MONTH FROM "createdAt") AS month, id, SUM(fee) AS fee | ||
FROM successful_extra | ||
GROUP BY year, month, id | ||
ORDER BY year, month, id; | ||
`; | ||
|
||
public static create = async ( | ||
extraFee: Omit<ExtraFeeType, 'fee'> & { fee?: number }, | ||
): Promise<void> => { | ||
await ExtraFee.create(extraFee); | ||
}; | ||
|
||
public static get = async (id: string): Promise<ExtraFeeType | null> => { | ||
return await ExtraFee.findByPk(id); | ||
}; | ||
|
||
public static setFee = async (id: string, fee: number): Promise<void> => { | ||
await ExtraFee.update({ fee }, { where: { swapId: id } }); | ||
}; | ||
|
||
public static getStats = async ( | ||
id: string, | ||
): Promise<GroupedByYearMonth<number>> => { | ||
const stats = (await Database.sequelize.query( | ||
{ | ||
query: ExtraFeeRepository.statsQuery, | ||
values: [ | ||
SwapUpdateEvent.TransactionClaimed, | ||
SwapUpdateEvent.InvoiceSettled, | ||
SwapUpdateEvent.TransactionClaimed, | ||
id, | ||
], | ||
}, | ||
{ | ||
type: QueryTypes.SELECT, | ||
}, | ||
)) as { year: number; month: number; id: string; fee: number }[]; | ||
|
||
const res = {}; | ||
|
||
stats.forEach((stat) => { | ||
const monthObj = getNestedObject( | ||
getNestedObject(res, stat.year), | ||
stat.month, | ||
); | ||
monthObj[stat.id] = Number(stat.fee); | ||
}); | ||
|
||
return res; | ||
}; | ||
} | ||
|
||
export default ExtraFeeRepository; |
Oops, something went wrong.