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: fix eligibility checks #296

Merged
merged 4 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 23 additions & 31 deletions background-jobs/update-rays-cron-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,27 +518,28 @@ function createChunksOfUserPointsDistributions(sortedPoints: PositionPoints, chu
async function checkMigrationEligibility(db: Kysely<Database>, positionPoints: PositionPoints) {
const existingPointDistributionsWithEligibilityCondition = await db
.selectFrom('pointsDistribution')
.where('positionId', '<>', null)
.leftJoin(
.select(['pointsDistribution.id as pointsId'])
.innerJoin(
'eligibilityCondition',
'eligibilityCondition.id',
'pointsDistribution.eligibilityConditionId',
)
.leftJoin('position', 'position.id', 'pointsDistribution.positionId')
.innerJoin('position', 'position.id', 'pointsDistribution.positionId')
.where('eligibilityCondition.type', '=', eligibilityConditions.POSITION_OPEN_TIME.type)
.where('pointsDistribution.type', '=', 'MIGRATION')
.selectAll()
.execute()

if (existingPointDistributionsWithEligibilityCondition.length > 0) {
await db.transaction().execute(async (transaction) => {
existingPointDistributionsWithEligibilityCondition.forEach(async (point) => {
if (
point.dueDate &&
point.type == eligibilityConditions.POSITION_OPEN_TIME.type &&
point.dueDate < new Date()
) {
for (const point of existingPointDistributionsWithEligibilityCondition) {
if (point.dueDate && point.dueDate < new Date()) {
const positionInSnapshot = positionPoints.find((p) => p.positionId === point.externalId)
if (!positionInSnapshot || positionInSnapshot.netValue <= 0) {
await transaction.deleteFrom('pointsDistribution').where('id', '=', point.id).execute()
await transaction
.deleteFrom('pointsDistribution')
.where('id', '=', point.pointsId)
.execute()
await transaction
.deleteFrom('eligibilityCondition')
.where('id', '=', point.eligibilityConditionId)
Expand All @@ -547,15 +548,15 @@ async function checkMigrationEligibility(db: Kysely<Database>, positionPoints: P
await transaction
.updateTable('pointsDistribution')
.set({ eligibilityConditionId: null })
.where('id', '=', point.id)
.where('id', '=', point.pointsId)
.execute()
await transaction
.deleteFrom('eligibilityCondition')
.where('id', '=', point.eligibilityConditionId)
.execute()
}
}
})
}
})
}
}
Expand Down Expand Up @@ -583,26 +584,21 @@ async function checkOpenedPositionEligibility(
// get all points distributions without an associated position id but with an eligibility condition
const existingUsersWithEligibilityCondition = await db
.selectFrom('pointsDistribution')
.where('eligibilityConditionId', '<>', null)
.where('positionId', '=', null)
.select(['pointsDistribution.id as pointsId'])
.leftJoin(
'eligibilityCondition',
'eligibilityCondition.id',
'pointsDistribution.eligibilityConditionId',
)
.leftJoin('userAddress', 'userAddress.id', 'pointsDistribution.userAddressId')
.where('eligibilityCondition.type', '=', eligibilityConditions.BECOME_SUMMER_USER.type)
.selectAll()
.execute()

if (existingUsersWithEligibilityCondition.length > 0) {
await db.transaction().execute(async (transaction) => {
existingUsersWithEligibilityCondition.forEach(async (user) => {
if (
user.dueDate &&
user.type == eligibilityConditions.BECOME_SUMMER_USER.type &&
user.dueDate >= new Date()
) {
// get all the positions of the user that are eligible for a check (exist in current points distribution)
for (const user of existingUsersWithEligibilityCondition) {
if (user.dueDate && user.dueDate >= new Date()) {
const eligiblePositionsFromPointsAccrual = positionPoints
.filter(
(p) =>
Expand All @@ -618,14 +614,14 @@ async function checkOpenedPositionEligibility(
const becomeSummerUserMultiplier = getBecomeSummerUserMultiplier(
oldestEligiblePosition.positionCreated,
)

const pointsDistributions = await transaction
.selectFrom('pointsDistribution')
.where('userAddressId', '=', user.id)
.where((eb) => eb('type', '=', 'Snapshot_General').or('type', '=', 'Snapshot_Defi'))
.selectAll()
.execute()
pointsDistributions.forEach(async (pointsDistribution) => {
for (const pointsDistribution of pointsDistributions) {
// update points distribution
await transaction
.updateTable('pointsDistribution')
.set({
Expand All @@ -634,14 +630,10 @@ async function checkOpenedPositionEligibility(
})
.where('id', '=', pointsDistribution.id)
.execute()
})
}
}
} else if (
user.dueDate &&
user.type == eligibilityConditions.BECOME_SUMMER_USER.type &&
user.dueDate < new Date()
) {
// if the due date is exceeded we delete all the points distribution and the eligibility condition
} else if (user.dueDate && user.dueDate < new Date()) {
// removes all points distributions and eligibility condition - there is one due date for all retro snapshot distributions
await transaction
.deleteFrom('pointsDistribution')
.where('eligibilityConditionId', '=', user.eligibilityConditionId)
Expand All @@ -651,7 +643,7 @@ async function checkOpenedPositionEligibility(
.where('id', '=', user.eligibilityConditionId)
.execute()
}
})
}
})
}
}
Expand Down
Loading