forked from snapshot-labs/snapshot-hub
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(persistence-interface): Introduce mongo-events-repository.ts
- Loading branch information
1 parent
e442a41
commit 109f91d
Showing
1 changed file
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { EventsRepository } from '../events-repository.js'; | ||
import { snapshotHubMongoDb } from '../../index.js'; | ||
import { Event } from '../../models/event.js'; | ||
|
||
const getExpiredEvents: ( | ||
timestamp: number | ||
) => Promise<Event[]> = async timestamp => { | ||
const db = await snapshotHubMongoDb; | ||
const eventsCollection = db.collection( | ||
process.env.MONGODB_TRIBUTE_DAOS_SNAPSHOT_HUB_DB_EVENTS_COLLECTION_NAME | ||
); | ||
return eventsCollection.find({ timestamp: { $lte: timestamp } }); | ||
}; | ||
|
||
const deleteProcessedEvent: (event: Event) => Promise<void> = async event => { | ||
const db = await snapshotHubMongoDb; | ||
const eventsCollection = db.collection( | ||
process.env.MONGODB_TRIBUTE_DAOS_SNAPSHOT_HUB_DB_EVENTS_COLLECTION_NAME | ||
); | ||
return eventsCollection.deleteOne({ id: event.id, event: event.event }); | ||
}; | ||
|
||
const insertCreatedProposal: ( | ||
EVENT_ID: string, | ||
space: string, | ||
timestamp: number | ||
) => Promise<void> = async (EVENT_ID, space, timestamp) => { | ||
const db = await snapshotHubMongoDb; | ||
const eventsCollection = db.collection( | ||
process.env.MONGODB_TRIBUTE_DAOS_SNAPSHOT_HUB_DB_EVENTS_COLLECTION_NAME | ||
); | ||
return eventsCollection.insertOne({ | ||
id: EVENT_ID, | ||
event: 'proposal/created', | ||
space: space, | ||
expire: timestamp | ||
}); | ||
}; | ||
|
||
const insertStartedProposal: ( | ||
EVENT_ID: string, | ||
space: string, | ||
timestamp: number | ||
) => Promise<void> = async (EVENT_ID, space, timestamp) => { | ||
const db = await snapshotHubMongoDb; | ||
const eventsCollection = db.collection( | ||
process.env.MONGODB_TRIBUTE_DAOS_SNAPSHOT_HUB_DB_EVENTS_COLLECTION_NAME | ||
); | ||
return eventsCollection.insertOne({ | ||
id: EVENT_ID, | ||
event: 'proposal/start', | ||
space: space, | ||
expire: timestamp | ||
}); | ||
}; | ||
|
||
const insertProposalEnd: ( | ||
EVENT_ID: string, | ||
space: string, | ||
timestamp: number | ||
) => Promise<void> = async (EVENT_ID, space, timestamp) => { | ||
const db = await snapshotHubMongoDb; | ||
const eventsCollection = db.collection( | ||
process.env.MONGODB_TRIBUTE_DAOS_SNAPSHOT_HUB_DB_EVENTS_COLLECTION_NAME | ||
); | ||
return eventsCollection.insertOne({ | ||
id: EVENT_ID, | ||
event: 'proposal/end', | ||
space: space, | ||
expire: timestamp | ||
}); | ||
}; | ||
|
||
|
||
export const mongoEventsRepository: EventsRepository = { | ||
getExpiredEvents, | ||
deleteProcessedEvent, | ||
insertCreatedProposal, | ||
insertStartedProposal, | ||
insertProposalEnd | ||
}; |