Agenda refactor
This commit is contained in:
parent
c549e34c8e
commit
ed580b8cc2
@ -181,7 +181,7 @@ export default async function CalendarPage() {
|
|||||||
for (const account of infomaniakAccounts) {
|
for (const account of infomaniakAccounts) {
|
||||||
// Check if a calendar sync already exists for this account (enabled or disabled)
|
// Check if a calendar sync already exists for this account (enabled or disabled)
|
||||||
// This prevents creating duplicate calendars for the same account
|
// This prevents creating duplicate calendars for the same account
|
||||||
const existingSync = await prisma.calendarSync.findFirst({
|
let existingSync = await prisma.calendarSync.findFirst({
|
||||||
where: {
|
where: {
|
||||||
mailCredentialId: account.id
|
mailCredentialId: account.id
|
||||||
},
|
},
|
||||||
@ -189,6 +189,48 @@ export default async function CalendarPage() {
|
|||||||
calendar: true
|
calendar: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If no sync found by mailCredentialId, check if there's an orphaned sync with same email
|
||||||
|
// This handles the case where user deleted and re-added the account (new mailCredentialId, same email)
|
||||||
|
if (!existingSync) {
|
||||||
|
const orphanedSyncByEmail = await prisma.calendarSync.findFirst({
|
||||||
|
where: {
|
||||||
|
provider: 'infomaniak',
|
||||||
|
calendar: {
|
||||||
|
userId: session?.user?.id || '',
|
||||||
|
name: 'Privée'
|
||||||
|
},
|
||||||
|
mailCredential: {
|
||||||
|
email: account.email
|
||||||
|
}
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
calendar: true,
|
||||||
|
mailCredential: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (orphanedSyncByEmail && orphanedSyncByEmail.mailCredential?.email === account.email) {
|
||||||
|
console.log(`[AGENDA] Found orphaned Infomaniak sync for email ${account.email}, reassigning to new mailCredentialId ${account.id}`);
|
||||||
|
// Reassign the sync to the new mailCredentialId
|
||||||
|
await prisma.calendarSync.update({
|
||||||
|
where: { id: orphanedSyncByEmail.id },
|
||||||
|
data: {
|
||||||
|
mailCredentialId: account.id,
|
||||||
|
syncEnabled: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Reload the sync
|
||||||
|
existingSync = await prisma.calendarSync.findFirst({
|
||||||
|
where: {
|
||||||
|
mailCredentialId: account.id
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
calendar: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If sync exists but is disabled, check if it's due to invalid credentials
|
// If sync exists but is disabled, check if it's due to invalid credentials
|
||||||
// Don't re-enable if the last error was 401 (invalid credentials)
|
// Don't re-enable if the last error was 401 (invalid credentials)
|
||||||
@ -567,6 +609,7 @@ export default async function CalendarPage() {
|
|||||||
// No default calendar creation - only synced calendars from courrier
|
// No default calendar creation - only synced calendars from courrier
|
||||||
|
|
||||||
// Debug: Verify Infomaniak calendars exist in database
|
// Debug: Verify Infomaniak calendars exist in database
|
||||||
|
// Check both by provider and by mailCredential email
|
||||||
const allInfomaniakSyncs = await prisma.calendarSync.findMany({
|
const allInfomaniakSyncs = await prisma.calendarSync.findMany({
|
||||||
where: {
|
where: {
|
||||||
provider: 'infomaniak',
|
provider: 'infomaniak',
|
||||||
@ -579,9 +622,30 @@ export default async function CalendarPage() {
|
|||||||
mailCredential: true
|
mailCredential: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(`[AGENDA] Found ${allInfomaniakSyncs.length} Infomaniak syncs in database`);
|
|
||||||
allInfomaniakSyncs.forEach(sync => {
|
// Also check for syncs linked to Infomaniak accounts by email (in case mailCredentialId changed)
|
||||||
console.log(`[AGENDA] Infomaniak sync: id=${sync.id}, calendarId=${sync.calendarId}, calendarName=${sync.calendar?.name}, syncEnabled=${sync.syncEnabled}, mailCredentialId=${sync.mailCredentialId}, hasMailCredential=${!!sync.mailCredential}`);
|
const infomaniakEmails = infomaniakAccounts.map(acc => acc.email);
|
||||||
|
const syncsByEmail = await prisma.calendarSync.findMany({
|
||||||
|
where: {
|
||||||
|
provider: 'infomaniak',
|
||||||
|
calendar: {
|
||||||
|
userId: session?.user?.id || ''
|
||||||
|
},
|
||||||
|
mailCredential: {
|
||||||
|
email: {
|
||||||
|
in: infomaniakEmails
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
calendar: true,
|
||||||
|
mailCredential: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`[AGENDA] Found ${allInfomaniakSyncs.length} Infomaniak syncs by provider, ${syncsByEmail.length} by email`);
|
||||||
|
[...allInfomaniakSyncs, ...syncsByEmail].forEach(sync => {
|
||||||
|
console.log(`[AGENDA] Infomaniak sync: id=${sync.id}, calendarId=${sync.calendarId}, calendarName=${sync.calendar?.name}, syncEnabled=${sync.syncEnabled}, mailCredentialId=${sync.mailCredentialId}, email=${sync.mailCredential?.email || 'none'}, hasMailCredential=${!!sync.mailCredential}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Debug: Log calendars before filtering
|
// Debug: Log calendars before filtering
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user