From 109f91daf8e89b3c7302237d83aec7aa8116f2ee Mon Sep 17 00:00:00 2001 From: Yousuf Haque Date: Tue, 10 Oct 2023 20:14:45 -0400 Subject: [PATCH] feat(persistence-interface): Introduce mongo-events-repository.ts --- .../mongo/mongo-events-repository.ts | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 server/repositories/mongo/mongo-events-repository.ts diff --git a/server/repositories/mongo/mongo-events-repository.ts b/server/repositories/mongo/mongo-events-repository.ts new file mode 100644 index 000000000..76b0e1efa --- /dev/null +++ b/server/repositories/mongo/mongo-events-repository.ts @@ -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 = 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 = 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 = 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 = 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 = 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 +};