import { randomUUID } from 'crypto' import { Organization } from '../types' import { mongodb } from './mongodb' const collection = mongodb.collection('organizations') export const createOrganization = async ( organization: Pick, ): Promise => { const model: Organization = { ...organization, accessKey: 'key:' + randomUUID(), createdAt: new Date(), } await collection.insertOne(model) return model } export const getOrganization = async (identity: string): Promise => { const organization = await collection.findOne({ identity }) if (!organization) { throw new Error('Organization not found') } return organization } export const getOrganizationsByMember = async (member: string): Promise => { return await collection .find({ members: member, }) .toArray() }