Skip to content

Commit d770e3c

Browse files
committed
feat: implement utility functions to streamline owner selection for backups and domains
1 parent d583ba8 commit d770e3c

1 file changed

Lines changed: 56 additions & 85 deletions

File tree

‎packages/server/src/services/overview.ts‎

Lines changed: 56 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ function ownerFrom(
276276
};
277277
}
278278

279+
// Picks the first non-null relation out of several mutually-exclusive owner candidates.
280+
// Shared by backups, volume backups, and domains — all resolve "which one of these
281+
// nullable relations is actually set" the same way.
282+
function pickFirst<TRow, TType extends string>(
283+
candidates: Array<[row: TRow | null | undefined, id: string | undefined, type: TType]>,
284+
): [TRow, string, TType] | null {
285+
for (const [row, id, type] of candidates) {
286+
if (row && id) return [row, id, type];
287+
}
288+
return null;
289+
}
290+
279291
function pickBackupOwner(backup: {
280292
postgres?: (OwnerRow & { postgresId: string }) | null;
281293
mariadb?: (OwnerRow & { mariadbId: string }) | null;
@@ -284,19 +296,15 @@ function pickBackupOwner(backup: {
284296
libsql?: (OwnerRow & { libsqlId: string }) | null;
285297
compose?: (OwnerRow & { composeId: string }) | null;
286298
}): Owner | null {
287-
if (backup.postgres)
288-
return ownerFrom(backup.postgres, backup.postgres.postgresId, "postgres");
289-
if (backup.mariadb)
290-
return ownerFrom(backup.mariadb, backup.mariadb.mariadbId, "mariadb");
291-
if (backup.mysql)
292-
return ownerFrom(backup.mysql, backup.mysql.mysqlId, "mysql");
293-
if (backup.mongo)
294-
return ownerFrom(backup.mongo, backup.mongo.mongoId, "mongo");
295-
if (backup.libsql)
296-
return ownerFrom(backup.libsql, backup.libsql.libsqlId, "libsql");
297-
if (backup.compose)
298-
return ownerFrom(backup.compose, backup.compose.composeId, "compose");
299-
return null;
299+
const found = pickFirst<OwnerRow, OverviewServiceType>([
300+
[backup.postgres, backup.postgres?.postgresId, "postgres"],
301+
[backup.mariadb, backup.mariadb?.mariadbId, "mariadb"],
302+
[backup.mysql, backup.mysql?.mysqlId, "mysql"],
303+
[backup.mongo, backup.mongo?.mongoId, "mongo"],
304+
[backup.libsql, backup.libsql?.libsqlId, "libsql"],
305+
[backup.compose, backup.compose?.composeId, "compose"],
306+
]);
307+
return found ? ownerFrom(...found) : null;
300308
}
301309

302310
function pickVolumeBackupOwner(volumeBackup: {
@@ -309,43 +317,17 @@ function pickVolumeBackupOwner(volumeBackup: {
309317
libsql?: (OwnerRow & { libsqlId: string }) | null;
310318
compose?: (OwnerRow & { composeId: string }) | null;
311319
}): Owner | null {
312-
if (volumeBackup.application)
313-
return ownerFrom(
314-
volumeBackup.application,
315-
volumeBackup.application.applicationId,
316-
"application",
317-
);
318-
if (volumeBackup.postgres)
319-
return ownerFrom(
320-
volumeBackup.postgres,
321-
volumeBackup.postgres.postgresId,
322-
"postgres",
323-
);
324-
if (volumeBackup.mariadb)
325-
return ownerFrom(
326-
volumeBackup.mariadb,
327-
volumeBackup.mariadb.mariadbId,
328-
"mariadb",
329-
);
330-
if (volumeBackup.mysql)
331-
return ownerFrom(volumeBackup.mysql, volumeBackup.mysql.mysqlId, "mysql");
332-
if (volumeBackup.mongo)
333-
return ownerFrom(volumeBackup.mongo, volumeBackup.mongo.mongoId, "mongo");
334-
if (volumeBackup.redis)
335-
return ownerFrom(volumeBackup.redis, volumeBackup.redis.redisId, "redis");
336-
if (volumeBackup.libsql)
337-
return ownerFrom(
338-
volumeBackup.libsql,
339-
volumeBackup.libsql.libsqlId,
340-
"libsql",
341-
);
342-
if (volumeBackup.compose)
343-
return ownerFrom(
344-
volumeBackup.compose,
345-
volumeBackup.compose.composeId,
346-
"compose",
347-
);
348-
return null;
320+
const found = pickFirst<OwnerRow, OverviewServiceType>([
321+
[volumeBackup.application, volumeBackup.application?.applicationId, "application"],
322+
[volumeBackup.postgres, volumeBackup.postgres?.postgresId, "postgres"],
323+
[volumeBackup.mariadb, volumeBackup.mariadb?.mariadbId, "mariadb"],
324+
[volumeBackup.mysql, volumeBackup.mysql?.mysqlId, "mysql"],
325+
[volumeBackup.mongo, volumeBackup.mongo?.mongoId, "mongo"],
326+
[volumeBackup.redis, volumeBackup.redis?.redisId, "redis"],
327+
[volumeBackup.libsql, volumeBackup.libsql?.libsqlId, "libsql"],
328+
[volumeBackup.compose, volumeBackup.compose?.composeId, "compose"],
329+
]);
330+
return found ? ownerFrom(...found) : null;
349331
}
350332

351333
// Ownerless backups (e.g. the Dokploy host's own database) are visible to everyone.
@@ -645,6 +627,17 @@ function domainOwnerFrom(
645627
};
646628
}
647629

630+
// Same scoping rule as ownerAccessCondition, just without the "ownerless rows are public" OR branch.
631+
function orgScopeCondition(
632+
orgId: string,
633+
idColumn: AnyPgColumn,
634+
accessedServices: string[] | null,
635+
) {
636+
return accessedServices !== null
637+
? and(eq(projects.organizationId, orgId), inArray(idColumn, accessedServices))
638+
: eq(projects.organizationId, orgId);
639+
}
640+
648641
// Preview domains inherit access from their parent application, so they're scoped by the same accessedServices check as applications.
649642
async function getDomainScopeIdsInOrg(
650643
orgId: string,
@@ -660,12 +653,7 @@ async function getDomainScopeIdsInOrg(
660653
)
661654
.innerJoin(projects, eq(environments.projectId, projects.projectId))
662655
.where(
663-
accessedServices !== null
664-
? and(
665-
eq(projects.organizationId, orgId),
666-
inArray(applications.applicationId, accessedServices),
667-
)
668-
: eq(projects.organizationId, orgId),
656+
orgScopeCondition(orgId, applications.applicationId, accessedServices),
669657
),
670658
db
671659
.select({ id: compose.composeId })
@@ -675,14 +663,7 @@ async function getDomainScopeIdsInOrg(
675663
eq(compose.environmentId, environments.environmentId),
676664
)
677665
.innerJoin(projects, eq(environments.projectId, projects.projectId))
678-
.where(
679-
accessedServices !== null
680-
? and(
681-
eq(projects.organizationId, orgId),
682-
inArray(compose.composeId, accessedServices),
683-
)
684-
: eq(projects.organizationId, orgId),
685-
),
666+
.where(orgScopeCondition(orgId, compose.composeId, accessedServices)),
686667
db
687668
.select({ id: previewDeployments.previewDeploymentId })
688669
.from(previewDeployments)
@@ -696,12 +677,7 @@ async function getDomainScopeIdsInOrg(
696677
)
697678
.innerJoin(projects, eq(environments.projectId, projects.projectId))
698679
.where(
699-
accessedServices !== null
700-
? and(
701-
eq(projects.organizationId, orgId),
702-
inArray(applications.applicationId, accessedServices),
703-
)
704-
: eq(projects.organizationId, orgId),
680+
orgScopeCondition(orgId, applications.applicationId, accessedServices),
705681
),
706682
]);
707683

@@ -765,21 +741,16 @@ export const getAllDomainsForOrganization = async (
765741

766742
const result: OverviewDomain[] = [];
767743
for (const domain of rows) {
768-
const owner = domain.application
769-
? domainOwnerFrom(
770-
domain.application,
771-
domain.application.applicationId,
772-
"application",
773-
)
774-
: domain.compose
775-
? domainOwnerFrom(domain.compose, domain.compose.composeId, "compose")
776-
: domain.previewDeployment?.application
777-
? domainOwnerFrom(
778-
domain.previewDeployment.application,
779-
domain.previewDeployment.application.applicationId,
780-
"application",
781-
)
782-
: null;
744+
const foundOwner = pickFirst<DomainOwnerRow, OverviewDomainOwnerType>([
745+
[domain.application, domain.application?.applicationId, "application"],
746+
[domain.compose, domain.compose?.composeId, "compose"],
747+
[
748+
domain.previewDeployment?.application,
749+
domain.previewDeployment?.application?.applicationId,
750+
"application",
751+
],
752+
]);
753+
const owner = foundOwner ? domainOwnerFrom(...foundOwner) : null;
783754

784755
if (!owner || owner.organizationId !== orgId) continue;
785756
if (accessedServices !== null && !accessedServices.includes(owner.id)) {

0 commit comments

Comments
 (0)