| 1 | import type { EnvironmentId, ProjectId, ServerSettings } from "@t3tools/contracts"; |
| 2 | |
| 3 | export type AutoSettleSettings = Pick< |
| 4 | ServerSettings, |
| 5 | "sidebarAutoSettleAfterDays" | "sidebarAutoSettleOnMerge" |
| 6 | >; |
| 7 | |
| 8 | interface AutoSettleSyncTarget { |
| 9 | readonly environmentId: EnvironmentId; |
| 10 | readonly projectId?: ProjectId | null; |
| 11 | readonly label: string; |
| 12 | readonly settings: AutoSettleSettings | null; |
| 13 | } |
| 14 | |
| 15 | /** Receives connected, capable targets. Applying these defaults must preserve other settings. */ |
| 16 | export function planAutoSettleSettingsSync( |
| 17 | reference: { |
| 18 | readonly environmentId: EnvironmentId; |
| 19 | readonly projectId?: ProjectId | null; |
| 20 | readonly settings: AutoSettleSettings; |
| 21 | }, |
| 22 | targets: readonly AutoSettleSyncTarget[], |
| 23 | ) { |
| 24 | const patch: AutoSettleSettings = { |
| 25 | sidebarAutoSettleAfterDays: reference.settings.sidebarAutoSettleAfterDays, |
| 26 | sidebarAutoSettleOnMerge: reference.settings.sidebarAutoSettleOnMerge, |
| 27 | }; |
| 28 | const mismatches = targets.filter( |
| 29 | (target) => |
| 30 | (target.environmentId !== reference.environmentId || |
| 31 | target.projectId !== reference.projectId) && |
| 32 | target.settings !== null && |
| 33 | (target.settings.sidebarAutoSettleAfterDays !== patch.sidebarAutoSettleAfterDays || |
| 34 | target.settings.sidebarAutoSettleOnMerge !== patch.sidebarAutoSettleOnMerge), |
| 35 | ); |
| 36 | return { patch, mismatches }; |
| 37 | } |