From 156d9f8edcb96d07e2a14e8ed4a8b6eda544e3b5 Mon Sep 17 00:00:00 2001 From: Gilad Resisi Date: Wed, 1 Jul 2026 22:11:53 +0700 Subject: [PATCH] fix(pinterest): keep analytics within the 90-day window Pinterest analytics endpoints only serve the last 90 days (evaluated in UTC). `postAnalytics` requested `start_date` 2 years ago, so every pin-analytics call failed with "You can only get data from the last 90 days", surfacing in prod as: Error fetching Pinterest post analytics: ApplicationFailure: Unknown Error (the generic "Unknown Error" is BadBody's default; Pinterest's real message was in the failure details). Changes: - postAnalytics: start_date now 89 days back instead of 2 years. Pinterest does not expose pin metrics older than 90 days, so 89 is the maximum obtainable; 89 (not 90) avoids an off-by-one when the server's local date leads UTC. - analytics (account level): clamp the requested period to <= 89 days for the same reason, so a longer UI selection can't trip the same error. - handleErrors: map "only get data from the last 90 days" to a clear message instead of the generic "Unknown Error". Introduced in 5cca81e0 ("feat: post analytics"). Co-Authored-By: Claude Opus 4.8 --- .../integrations/social/pinterest.provider.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/libraries/nestjs-libraries/src/integrations/social/pinterest.provider.ts b/libraries/nestjs-libraries/src/integrations/social/pinterest.provider.ts index a647f61e2d..5f6f7d6fde 100644 --- a/libraries/nestjs-libraries/src/integrations/social/pinterest.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/pinterest.provider.ts @@ -122,6 +122,12 @@ export class PinterestProvider 'When uploading a video, you must add also an image to be used as a cover image.', }; } + if (body.indexOf('only get data from the last 90 days') > -1) { + return { + type: 'bad-body' as const, + value: 'Pinterest analytics are only available for the last 90 days.', + }; + } return undefined; } @@ -391,7 +397,12 @@ export class PinterestProvider date: number ): Promise { const until = dayjs().format('YYYY-MM-DD'); - const since = dayjs().subtract(date, 'day').format('YYYY-MM-DD'); + // Pinterest analytics only cover the last 90 days; clamp so a longer + // requested period doesn't fail with "You can only get data from the last + // 90 days" (89 for a UTC-boundary safety margin). + const since = dayjs() + .subtract(Math.min(date, 89), 'day') + .format('YYYY-MM-DD'); const { all: { daily_metrics }, @@ -456,8 +467,11 @@ export class PinterestProvider date: number ): Promise { const today = dayjs().format('YYYY-MM-DD'); - // Use a very long date range (2 years) to capture lifetime metrics for older posts - const since = dayjs().subtract(2, 'year').format('YYYY-MM-DD'); + // Pinterest only serves pin analytics for the last 90 days; an older + // start_date fails with "You can only get data from the last 90 days". + // Use 89 to stay safely inside the window (Pinterest evaluates it in UTC, + // so exactly 90 can trip an off-by-one when the server's local date is ahead). + const since = dayjs().subtract(89, 'day').format('YYYY-MM-DD'); try { // Fetch pin analytics from Pinterest API