mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
fix(docsync): an orphaned binding stays stopped on every path that starts a run
This commit is contained in:
@@ -411,6 +411,14 @@ export class DocSyncConfigService {
|
||||
updateLink(id: number, patch: Partial<DocsyncLinkInput>): LinkRow | undefined {
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
// Sync automatically is the way back for an orphaned binding, but only once
|
||||
// the person whose credential it runs under is on the trip again. Until
|
||||
// then the switch stays off rather than handing that credential back.
|
||||
const current = patch.syncEnabled === true ? this.getLink(id) : undefined;
|
||||
if (current?.last_sync_state === 'orphaned') {
|
||||
if (this.ownerLeft(current.connection_id)) patch = { ...patch, syncEnabled: undefined };
|
||||
else sets.push("last_sync_state = 'never'");
|
||||
}
|
||||
if (patch.direction !== undefined) { sets.push('direction = ?'); values.push(patch.direction); }
|
||||
if (patch.deletePolicy !== undefined) { sets.push('delete_policy = ?'); values.push(patch.deletePolicy); }
|
||||
if (patch.conflictPolicy !== undefined) { sets.push('conflict_policy = ?'); values.push(patch.conflictPolicy); }
|
||||
@@ -467,6 +475,29 @@ export class DocSyncConfigService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a binding must not run. The sweep below marks it, but only while
|
||||
* Sync automatically is on, so a paused binding whose owner left is not
|
||||
* marked; every path that starts a run asks here rather than reading the
|
||||
* mark alone.
|
||||
*/
|
||||
isOrphaned(link: LinkRow): boolean {
|
||||
return link.last_sync_state === 'orphaned' || this.ownerLeft(link.connection_id);
|
||||
}
|
||||
|
||||
private ownerLeft(connectionId: number): boolean {
|
||||
return !!this.db.connection
|
||||
.prepare(
|
||||
`SELECT 1 FROM document_connections c
|
||||
WHERE c.id = ?
|
||||
AND c.owner_user_id NOT IN (
|
||||
SELECT tm.user_id FROM trip_members tm WHERE tm.trip_id = c.trip_id
|
||||
UNION SELECT t.user_id FROM trips t WHERE t.id = c.trip_id
|
||||
)`,
|
||||
)
|
||||
.get(connectionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bindings whose credential owner is no longer on the trip.
|
||||
*
|
||||
|
||||
@@ -354,10 +354,9 @@ export class DocSyncController {
|
||||
const link = this.config.getLink(Number(linkId));
|
||||
if (!link || link.trip_id !== Number(tripId)) throw new HttpException('Link not found', 404);
|
||||
// An orphaned binding stays orphaned: its credential belongs to somebody who
|
||||
// is no longer on this trip, and a manual run would use it anyway. The
|
||||
// scheduler already refuses it (sync_enabled is 0), so without this the
|
||||
// button was a way around that.
|
||||
if (link.last_sync_state === 'orphaned') {
|
||||
// is no longer on this trip. The run itself would stand down as well; this
|
||||
// says why, instead of answering with an empty run.
|
||||
if (this.config.isOrphaned(link)) {
|
||||
throw new HttpException({ error: 'This binding lost its owner and has to be reconnected' }, 409);
|
||||
}
|
||||
// Refused before the shelved rows are touched, so a binding an admin
|
||||
|
||||
@@ -101,7 +101,7 @@ export class DocSyncMcp {
|
||||
// An orphaned binding stays stopped, as it does on the REST route and for
|
||||
// the scheduler: its credential belongs to somebody who has left the trip,
|
||||
// and a run would use it anyway.
|
||||
if (link.last_sync_state === 'orphaned') {
|
||||
if (this.config.isOrphaned(link)) {
|
||||
results.push({ linkId: link.id, provider: link.provider_id, state: 'orphaned', errorCode: 'orphaned' });
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -112,6 +112,7 @@ export class DocSyncService {
|
||||
.prepare(
|
||||
`SELECT * FROM trip_document_links
|
||||
WHERE sync_enabled = 1
|
||||
AND last_sync_state != 'orphaned'
|
||||
AND failure_count < ?
|
||||
AND (next_attempt_at IS NULL OR next_attempt_at <= CURRENT_TIMESTAMP)
|
||||
AND provider_id IN (SELECT id FROM document_providers WHERE enabled = 1)
|
||||
@@ -176,6 +177,12 @@ export class DocSyncService {
|
||||
if (this.isSwitchedOff(link)) {
|
||||
return { state: 'disabled', pulled: 0, pushed: 0, conflicts: 0, missing: 0 };
|
||||
}
|
||||
// An orphaned binding runs for nobody. Its credential belongs to somebody
|
||||
// who has left the trip, and a webhook or a resolved conflict reach this
|
||||
// without asking beforehand.
|
||||
if (this.config.isOrphaned(link)) {
|
||||
return { state: 'orphaned', pulled: 0, pushed: 0, conflicts: 0, missing: 0 };
|
||||
}
|
||||
const conn = this.config.getConnection(link.connection_id);
|
||||
if (!conn) {
|
||||
this.recordLinkFailure(link, 'not_found');
|
||||
|
||||
Reference in New Issue
Block a user