feat: added router and updated available service

This commit is contained in:
=
2025-11-13 12:52:38 +05:30
parent 98e2be477b
commit c63327e56e
5 changed files with 871 additions and 34 deletions
@@ -92,6 +92,7 @@ export const membershipIdentityDALFactory = (db: TDbClient) => {
db.ref("name").withSchema(TableName.Identity).as("identityName"),
db.ref("id").withSchema(TableName.Identity).as("identityId"),
db.ref("orgId").withSchema(TableName.Identity).as("identityOrgId"),
db.ref("projectId").withSchema(TableName.Identity).as("identityProjectId"),
db.ref("hasDeleteProtection").withSchema(TableName.Identity).as("identityHasDeleteProtection"),
db.ref("slug").withSchema(TableName.Role).as("roleSlug"),
@@ -134,6 +135,7 @@ export const membershipIdentityDALFactory = (db: TDbClient) => {
const {
identityId: actorIdentityId,
identityOrgId,
identityProjectId,
identityHasDeleteProtection,
identityName,
uaId,
@@ -155,7 +157,8 @@ export const membershipIdentityDALFactory = (db: TDbClient) => {
name: identityName,
id: actorIdentityId,
hasDeleteProtection: identityHasDeleteProtection,
identityOrgId,
orgId: identityOrgId,
projectId: identityProjectId,
authMethods: buildAuthMethods({
uaId,
awsId,
@@ -281,6 +284,8 @@ export const membershipIdentityDALFactory = (db: TDbClient) => {
.select(
db.ref("name").withSchema(TableName.Identity).as("identityName"),
db.ref("id").withSchema(TableName.Identity).as("identityId"),
db.ref("orgId").withSchema(TableName.Identity).as("identityOrgId"),
db.ref("projectId").withSchema(TableName.Identity).as("identityProjectId"),
db.ref("hasDeleteProtection").withSchema(TableName.Identity).as("identityHasDeleteProtection"),
db.ref("slug").withSchema(TableName.Role).as("roleSlug"),
@@ -310,13 +315,22 @@ export const membershipIdentityDALFactory = (db: TDbClient) => {
data: docs,
key: "id",
parentMapper: (el) => {
const { identityId: actorIdentityId, identityHasDeleteProtection, identityName } = el;
const {
identityId: actorIdentityId,
identityHasDeleteProtection,
identityName,
identityProjectId,
identityOrgId
} = el;
return {
...MembershipsSchema.parse(el),
identityId: actorIdentityId,
identity: {
name: identityName,
id: actorIdentityId,
hasDeleteProtection: identityHasDeleteProtection
hasDeleteProtection: identityHasDeleteProtection,
orgId: identityOrgId,
projectId: identityProjectId
}
};
},
@@ -356,14 +370,18 @@ export const membershipIdentityDALFactory = (db: TDbClient) => {
}
};
// this right now only support sub organization
const listAvailableIdentities = async (orgId: string, rootOrgId: string) => {
const listAvailableIdentities = async (scopeData: AccessScopeData, rootOrgId: string) => {
try {
const usersConnectedToOrg = db
const identitesConnectedToOrg = db
.replicaNode()(TableName.Membership)
.whereNotNull(`${TableName.Membership}.actorIdentityId`)
.where(`${TableName.Membership}.scope`, AccessScope.Organization)
.where(`${TableName.Membership}.scopeOrgId`, orgId)
.where(`${TableName.Membership}.scopeOrgId`, scopeData.orgId)
.where(`${TableName.Membership}.scope`, scopeData.scope)
.where((qb) => {
if (scopeData.scope === AccessScope.Project) {
void qb.where(`${TableName.Membership}.scopeProjectId`, scopeData.projectId);
}
})
.select("actorIdentityId");
const docs = await db
@@ -371,8 +389,15 @@ export const membershipIdentityDALFactory = (db: TDbClient) => {
.join(TableName.Identity, `${TableName.Identity}.id`, `${TableName.Membership}.actorIdentityId`)
.where(`${TableName.Membership}.scope`, AccessScope.Organization)
.whereNotNull(`${TableName.Membership}.actorIdentityId`)
.where(`${TableName.Membership}.scopeOrgId`, rootOrgId)
.whereNotIn(`${TableName.Membership}.actorIdentityId`, usersConnectedToOrg)
.where((qb) => {
// if sub org pick from root and if project pick from org of project
if (scopeData.scope === AccessScope.Organization) {
void qb.where(`${TableName.Membership}.scopeOrgId`, rootOrgId);
} else {
void qb.where(`${TableName.Membership}.scopeOrgId`, scopeData.orgId);
}
})
.whereNotIn(`${TableName.Membership}.actorIdentityId`, identitesConnectedToOrg)
.select(
db.ref("id").withSchema(TableName.Identity),
db.ref("name").withSchema(TableName.Identity),
@@ -340,13 +340,9 @@ export const membershipIdentityServiceFactory = ({
await factory.onListMembershipIdentityGuard(dto);
const organizationDetails = await orgDAL.findById(dto.scopeData.orgId);
if (!organizationDetails.rootOrgId) return { identities: [] };
if (dto.permission.rootOrgId === dto.permission.orgId) return { identities: [] };
const identities = await membershipIdentityDAL.listAvailableIdentities(
organizationDetails.id,
organizationDetails.rootOrgId
);
const identities = await membershipIdentityDAL.listAvailableIdentities(dto.scopeData, dto.permission.rootOrgId);
return { identities };
};