import { GroupModel } from "../models/GroupModel" ; export async function getAllGroups ( organization ) { const doc = await GroupModel . findOne ( { organization , } ) ; if ( doc ) { return doc . groups ; } return [ ] ; } export async function addGroups ( organization , groups ) { groups = groups . filter ( ( x ) => x . length > 1 ) ; if ( ! groups . length ) return ; await GroupModel . updateOne ( { organization } , { $addToSet : { groups : { $each : groups } , } , } , { upsert : true , } ) ; } export async function addGroupsDiff ( organization , oldGroups , newGroups ) { const diff = newGroups . filter ( ( x ) => ! oldGroups . includes ( x ) ) ; if ( diff . length ) { await addGroups ( organization , diff ) ; } }