-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make boostings of a project zero when get cancelled
related to Giveth/giveth-dapps-v2#1837
- Loading branch information
1 parent
1d757c6
commit b1686ef
Showing
5 changed files
with
94 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
createProjectData, | ||
generateRandomEtheriumAddress, | ||
saveProjectDirectlyToDb, | ||
saveUserDirectlyToDb, | ||
sleep, | ||
} from '../../test/testUtils'; | ||
import { | ||
findUserPowerBoosting, | ||
setMultipleBoosting, | ||
} from '../repositories/powerBoostingRepository'; | ||
import { assert } from 'chai'; | ||
import { changeUserBoostingsAfterProjectCancelled } from './powerBoostingService'; | ||
|
||
describe( | ||
'changeUserBoostingsAfterProjectCancelled', | ||
changeUserBoostingsAfterProjectCancelledTestCases, | ||
); | ||
|
||
function changeUserBoostingsAfterProjectCancelledTestCases() { | ||
it('should change user percentage to zero when project cancelled', async () => { | ||
const user1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); | ||
const user2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); | ||
const firstProject = await saveProjectDirectlyToDb(createProjectData()); | ||
const projectThatWouldGetCancelled = await saveProjectDirectlyToDb( | ||
createProjectData(), | ||
); | ||
await setMultipleBoosting({ | ||
userId: user1.id, | ||
projectIds: [firstProject.id, projectThatWouldGetCancelled.id], | ||
percentages: [80, 20], | ||
}); | ||
await setMultipleBoosting({ | ||
userId: user2.id, | ||
projectIds: [firstProject.id, projectThatWouldGetCancelled.id], | ||
percentages: [70, 30], | ||
}); | ||
await changeUserBoostingsAfterProjectCancelled({ | ||
projectId: projectThatWouldGetCancelled.id, | ||
}); | ||
|
||
// Changing percentages is async we sleep some milli seconds to make sure all updates has been done | ||
await sleep(100); | ||
const firstUserBoostings = await findUserPowerBoosting(user1.id); | ||
const secondUserBoostings = await findUserPowerBoosting(user2.id); | ||
|
||
assert.equal(firstUserBoostings.length, 1); | ||
assert.equal(secondUserBoostings.length, 1); | ||
assert.equal(firstUserBoostings[0].percentage, 100); | ||
assert.equal(firstUserBoostings[0].projectId, firstProject.id); | ||
assert.equal(secondUserBoostings[0].percentage, 100); | ||
assert.equal(secondUserBoostings[0].projectId, firstProject.id); | ||
}); | ||
} |
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,21 @@ | ||
import { | ||
findPowerBoostings, | ||
setSingleBoosting, | ||
} from '../repositories/powerBoostingRepository'; | ||
|
||
export const changeUserBoostingsAfterProjectCancelled = async (params: { | ||
projectId: number; | ||
}) => { | ||
const { projectId } = params; | ||
const [powerBoostings] = await findPowerBoostings({ | ||
orderBy: { direction: 'DESC', field: 'createdAt' }, | ||
projectId, | ||
}); | ||
powerBoostings.forEach(powerBoosting => | ||
setSingleBoosting({ | ||
userId: powerBoosting.userId, | ||
projectId, | ||
percentage: 0, | ||
}), | ||
); | ||
}; |