Skip to content

Commit

Permalink
feat(persistence-interface): Introduce mongo-events-repository.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
yousuf-haque committed Oct 11, 2023
1 parent e442a41 commit 109f91d
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions server/repositories/mongo/mongo-events-repository.ts
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
};

0 comments on commit 109f91d

Please sign in to comment.