Skip to content

Commit 5b3ba42

Browse files
authored
Merge pull request #2651 from ankaboot-source/docs/update-readme-features
fix(campaigns): refactor token expiration check and fix schema issue
2 parents 4953f2d + c77c402 commit 5b3ba42

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

supabase/functions/email-campaigns/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { resolveCampaignBaseUrlFromEnv } from "../_shared/url.ts";
1111
import { sendEmail, verifyTransport } from "./email.ts";
1212
import {
1313
getSenderCredentialIssue,
14+
isTokenExpired,
1415
listUniqueSenderSources,
1516
refreshOAuthToken,
1617
updateMiningSourceCredentials,
@@ -662,19 +663,15 @@ async function resolveSenderOptions(authorization: string, userEmail: string) {
662663
}
663664

664665
for (const source of sources) {
665-
const expiresAt = source.credentials.expiresAt;
666666
const nowMs = Date.now();
667+
const expired = isTokenExpired(source.credentials, nowMs);
667668
console.log(
668669
"[OAuth] Checking source:",
669670
source.email,
670671
"- type:",
671672
source.type,
672-
"- expiresAt:",
673-
expiresAt,
674-
"- now:",
675-
nowMs,
676673
"- isExpired:",
677-
expiresAt && expiresAt <= nowMs,
674+
expired,
678675
);
679676

680677
const credentialIssue = getSenderCredentialIssue(source);

supabase/functions/email-campaigns/sender-options.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export async function updateMiningSourceCredentials(
132132
}
133133

134134
const { error } = await supabaseAdmin
135+
.schema("private")
135136
.from("mining_sources")
136137
.update({ credentials })
137138
.eq("email", email);
@@ -169,24 +170,13 @@ export function getSenderCredentialIssue(
169170
return null;
170171
}
171172

172-
let expiresAt = source.credentials.expiresAt;
173-
174-
// Handle both numeric timestamp and ISO date string
175-
if (typeof expiresAt === "string") {
176-
expiresAt = new Date(expiresAt).getTime();
177-
}
178-
179-
const numericExpiresAt = Number(expiresAt);
180-
if (
181-
Number.isFinite(numericExpiresAt) &&
182-
numericExpiresAt > 0 &&
183-
numericExpiresAt <= nowMs
184-
) {
173+
const expired = isTokenExpired(source.credentials, nowMs);
174+
if (expired) {
185175
console.log(
186176
"[OAuth] Token IS EXPIRED for:",
187177
source.email,
188178
"- expiresAt:",
189-
numericExpiresAt,
179+
source.credentials.expiresAt,
190180
"- now:",
191181
nowMs,
192182
);
@@ -195,3 +185,22 @@ export function getSenderCredentialIssue(
195185

196186
return null;
197187
}
188+
189+
export function isTokenExpired(
190+
credentials: Record<string, unknown>,
191+
nowMs = Date.now(),
192+
): boolean {
193+
let expiresAt = credentials.expiresAt;
194+
195+
// Handle both numeric timestamp and ISO date string
196+
if (typeof expiresAt === "string") {
197+
expiresAt = new Date(expiresAt).getTime();
198+
}
199+
200+
const numericExpiresAt = Number(expiresAt);
201+
return (
202+
Number.isFinite(numericExpiresAt) &&
203+
numericExpiresAt > 0 &&
204+
numericExpiresAt <= nowMs
205+
);
206+
}

0 commit comments

Comments
 (0)