( rows: WorkspaceCleanupScopeRow[] )
| 134 | } |
| 135 | |
| 136 | async function resolvePlanTypesByWorkspaceId( |
| 137 | rows: WorkspaceCleanupScopeRow[] |
| 138 | ): Promise<Map<string, PlanCategory>> { |
| 139 | const userScopedRows = rows.filter((row) => row.workspaceMode !== WORKSPACE_MODE.ORGANIZATION) |
| 140 | const userPlanByBilledUserId = await resolvePersonalPlanTypesByBilledUserId(userScopedRows) |
| 141 | const entries = await Promise.all( |
| 142 | rows.map(async (row) => { |
| 143 | if (row.workspaceMode === WORKSPACE_MODE.ORGANIZATION) { |
| 144 | const organizationId = isOrganizationWorkspace(row) ? row.organizationId : null |
| 145 | if (!organizationId) { |
| 146 | logger.error('Skipping cleanup for malformed organization workspace', { |
| 147 | workspaceId: row.id, |
| 148 | organizationId: row.organizationId, |
| 149 | }) |
| 150 | return null |
| 151 | } |
| 152 | |
| 153 | try { |
| 154 | const subscription = await getOrganizationSubscription(organizationId, { |
| 155 | onError: 'throw', |
| 156 | }) |
| 157 | if (!subscription) { |
| 158 | logger.warn('Skipping cleanup for organization workspace without an org subscription', { |
| 159 | workspaceId: row.id, |
| 160 | organizationId, |
| 161 | }) |
| 162 | return null |
| 163 | } |
| 164 | |
| 165 | return [row.id, getPlanType(subscription?.plan)] as const |
| 166 | } catch (error) { |
| 167 | logger.error('Skipping cleanup for organization workspace after plan lookup failed', { |
| 168 | workspaceId: row.id, |
| 169 | organizationId, |
| 170 | error, |
| 171 | }) |
| 172 | return null |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | const plan = userPlanByBilledUserId.get(row.billedAccountUserId) |
| 177 | if (plan === undefined) { |
| 178 | return null |
| 179 | } |
| 180 | |
| 181 | return [row.id, plan] as const |
| 182 | }) |
| 183 | ) |
| 184 | |
| 185 | return new Map(entries.filter((entry): entry is PlanResolutionEntry => entry !== null)) |
| 186 | } |
| 187 | |
| 188 | async function buildCleanupRunner(jobType: CleanupJobType): Promise<EnqueueOptions['runner']> { |
| 189 | const cleanupRunner = await (async () => { |
no test coverage detected