import { IGroup , IUser , IUsersStorage , IUserWithGroups , IUserWithRoles , UserType , } from "@staticdeploy/core" ; import { defaults , find , flatMap , omit , toArray } from "lodash" ; import cloneMethodsIO from "./common/cloneMethodsIO" ; import convertErrors from "./common/convertErrors" ; import { ICollection } from "./common/ICollection" ; @ cloneMethodsIO @ convertErrors export default class UsersStorage implements IUsersStorage { private static omitGroupsIds ( user ) { return omit ( user , "groupsIds" ) ; } constructor ( private users , private groups ) { } async findOne ( id ) { const user = this . users [ id ] ; return user ? UsersStorage . omitGroupsIds ( user ) : null ; } async findOneWithGroups ( id ) { const user = this . users [ id ] ; return user ? { ... UsersStorage . omitGroupsIds ( user ) , groups : user . groupsIds . map ( ( groupId ) => this . groups [ groupId ] ) , } : null ; } async findOneWithRolesByIdpAndIdpId ( idp , idpId ) { const user = find ( this . users , { idp , idpId } ) ; return user ? { ... UsersStorage . omitGroupsIds ( user ) , roles : flatMap ( user . groupsIds , ( groupId ) => this . groups [ groupId ] . roles ) , } : null ; } async findMany ( ) { return toArray ( this . users ) . map ( UsersStorage . omitGroupsIds ) ; } async oneExistsWithIdpAndIdpId ( idp , idpId ) { return ! ! find ( this . users , { idp , idpId } ) ; } async anyExistsWithGroup ( groupId ) { return ! ! find ( this . users , ( u ) => u . groupsIds . includes ( groupId ) ) ; } async createOne ( toBeCreatedUser ) { this . users [ toBeCreatedUser . id ] = toBeCreatedUser ; return UsersStorage . omitGroupsIds ( toBeCreatedUser ) ; } async updateOne ( id , patch ) { this . users [ id ] = { ... this . users [ id ] , ... defaults ( patch , this . users [ id ] ) , } ; return UsersStorage . omitGroupsIds ( this . users [ id ] ) ; } async deleteOne ( id ) { delete this . users [ id ] ; } }