-
Notifications
You must be signed in to change notification settings - Fork 48
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
c8709c9
commit f9d3acc
Showing
9 changed files
with
336 additions
and
20 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,76 @@ | ||
import { QueryTypes } from 'sequelize'; | ||
import { SwapUpdateEvent } from '../../consts/Enums'; | ||
import { getNestedObject } from '../../data/Utils'; | ||
import Database from '../Database'; | ||
import ExtraFee, { ExtraFeeType } from '../models/ExtraFee'; | ||
|
||
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<Record<string, Record<string, 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] = stat.fee; | ||
}); | ||
|
||
return res; | ||
}; | ||
} | ||
|
||
export default ExtraFeeRepository; |
Oops, something went wrong.