1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 05:15:02 +00:00
mon-entreprise/server/source/mongodb.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-10-05 12:56:06 +02:00
import { MongoClient } from 'mongodb'
import { MONGO_URL } from './config.js'
interface OAuthCollection {
accessToken: string
refreshToken: string
}
interface MemberIds {
memberIds: string[]
}
export const initMongodb = async () => {
const client = new MongoClient(MONGO_URL)
await client.connect()
const db = client.db('bot')
return {
saveOAuth: ({ accessToken, refreshToken }: OAuthCollection) => {
const collection = db.collection<OAuthCollection>('oauth')
return collection.findOneAndReplace(
{},
{ accessToken, refreshToken },
{ upsert: true }
)
},
getOAuth: () => {
const collection = db.collection<OAuthCollection>('oauth')
return collection.findOne()
},
setWeeklyTeamOrder: (memberIds: string[]) => {
const collection = db.collection<MemberIds>('weeklyTeamOrder')
return collection.findOneAndReplace({}, { memberIds }, { upsert: true })
},
getWeeklyTeamOrder: () => {
const collection = db.collection<MemberIds>('weeklyTeamOrder')
return collection.findOne()
},
close: () => client.close(),
}
}