Skip to content

Commit

Permalink
build fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryansinha16 committed Nov 18, 2024
1 parent bae7869 commit 0be2850
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 72 deletions.
64 changes: 18 additions & 46 deletions packages/channel-google/src/channel-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { logger } from '@repo/logger';
import { type Request as ExpressRequest, type Response as ExpressResponse } from 'express';
import {
type AdAccountIntegration,
type AdWithAdAccount,
authEndpoint,
type ChannelAd,
type ChannelCreative,
Expand Down Expand Up @@ -183,38 +182,27 @@ class Google implements ChannelInterface {
params.append('client_secret', env.GOOGLE_CHANNEL_APPLICATION_SECRET);
params.append('refresh_token', integration.refreshToken);

interface ResponseData {
access_token: string;
expires_in: number;
}

const response: ResponseData = await fetch(url, {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params.toString(),
})
.then((res) => res.json() as Promise<ResponseData>)
.catch(() => {
throw new AError(`Failed to refresh token`);
});
}).catch(() => {
throw new AError(`Failed to refresh token`);
});

if (response instanceof Error) return response;

const tokenData: unknown = await response.json();

const schema = z.object({
access_token: z.string(),
expires_in: z.number().int(),
refresh_token: z.string(),
refresh_token_expires_in: z.null(),
scope: z.string(),
});

const updatedToken = {
...response,
refresh_token: integration.refreshToken,
refresh_token_expires_in: null,
};

const parsed = schema.safeParse(updatedToken);
const parsed = schema.safeParse(tokenData);
if (!parsed.success) {
logger.error(parsed.error, 'Failed to parse refresh access token response');
return new AError('Failed to parse refresh access token response');
Expand All @@ -223,16 +211,13 @@ class Google implements ChannelInterface {
return await updateIntegrationTokens(integration, {
accessToken: parsed.data.access_token,
accessTokenExpiresAt: addInterval(new Date(), 'seconds', parsed.data.expires_in),
refreshToken: parsed.data.refresh_token,
refreshTokenExpiresAt: addInterval(new Date(), 'seconds', 7776000000), // TODO: Add dynamic expire time
refreshToken: integration.refreshToken,
refreshTokenExpiresAt: addInterval(new Date(), 'seconds', 7776000000),
});
}

async refreshedIntegration(integration: Integration): Promise<Integration | AError> {
const expiresAt =
typeof integration.accessTokenExpiresAt === 'string'
? new Date(integration.accessTokenExpiresAt)
: integration.accessTokenExpiresAt;
const expiresAt = integration.accessTokenExpiresAt;

const oneHourFromNow = addInterval(new Date(), 'seconds', 60 * 60);

Expand Down Expand Up @@ -268,6 +253,8 @@ class Google implements ChannelInterface {
ad_group_ad.ad.id,
ad_group_ad.ad.name,
ad_group_ad.ad.video_responsive_ad.call_to_actions,
ad_group_ad.ad.video_responsive_ad.videos,
ad_group_ad.ad.video_responsive_ad.descriptions,
ad_group.id,
ad_group.name,
Expand Down Expand Up @@ -314,14 +301,8 @@ class Google implements ChannelInterface {
const query = `
SELECT
asset.id,
asset.name,
asset.type,
asset.resource_name,
asset.youtube_video_asset.youtube_video_id,
asset.image_asset.file_size,
asset.image_asset.full_size.url,
asset.image_asset.mime_type,
asset.text_asset.text
asset.youtube_video_asset.youtube_video_id
FROM
asset
WHERE
Expand Down Expand Up @@ -354,9 +335,9 @@ class Google implements ChannelInterface {
externalId: c.asset.id,
adAccountId: dbAccount.id,
name: c.asset.resourceName,
body: el.adGroupAd.ad.videoResponsiveAd?.descriptions[0].text,
body: el.adGroupAd.ad.videoResponsiveAd?.descriptions?.[0].text,
title: el.video.title,
callToActionType: el.adGroupAd.ad.videoResponsiveAd?.callToActions[0].text,
callToActionType: el.adGroupAd.ad.videoResponsiveAd?.callToActions?.[0].text,
imageUrl: c.asset.youtubeVideoAsset?.youtubeVideoId,
}));

Expand All @@ -373,7 +354,6 @@ class Google implements ChannelInterface {
ads,
new Map<string, string>(),
creatives,
new Map<string, string>(),
[],
);
} catch (err) {
Expand Down Expand Up @@ -455,8 +435,6 @@ class Google implements ChannelInterface {
const defaultQuery = `
SELECT
customer_client.client_customer,
customer_client.level,
customer_client.manager,
customer_client.descriptive_name
FROM
customer_client
Expand Down Expand Up @@ -531,10 +509,6 @@ class Google implements ChannelInterface {
return Promise.reject(new AError('Not Implemented'));
}

async saveCreatives(_integration: Integration, _groupByAdAccount: Map<string, AdWithAdAccount[]>): Promise<void> {
return Promise.reject(new AError('Not Implemented'));
}

async processReport(
_adAccount: AdAccountIntegration,
_taskId: string,
Expand Down Expand Up @@ -609,9 +583,7 @@ class Google implements ChannelInterface {
const query = `
SELECT
customer_client.client_customer,
customer_client.level,
customer_client.manager,
customer_client.descriptive_name
customer_client.manager
FROM
customer_client
`;
Expand Down
42 changes: 16 additions & 26 deletions packages/channel-google/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,25 @@ const videoSchema = z.object({
});

const videoResponsiveAdSchema = z.object({
headlines: z.array(
z.object({
text: z.string(),
}),
),
longHeadlines: z.array(
z.object({
text: z.string(),
}),
),
descriptions: z.array(
z.object({
text: z.string(),
}),
),
callToActions: z.array(
z.object({
text: z.string(),
}),
),
descriptions: z
.array(
z.object({
text: z.string(),
}),
)
.optional(),
callToActions: z
.array(
z.object({
text: z.string(),
}),
)
.optional(),
videos: z.array(
z.object({
asset: z.string(),
}),
),
breadcrumb1: z.string().optional(), // Optional field
breadcrumb2: z.string().optional(), // Potentially optional for future cases
});

const youtubeAdSchema = z.object({
Expand Down Expand Up @@ -83,10 +75,9 @@ export const videoAdResponseSchema = createCommonResponseSchema(
);

const customerClientSchema = z.object({
resourceName: z.string(),
resourceName: z.string().optional(),
clientCustomer: z.string(),
level: z.string(),
manager: z.boolean(),
manager: z.boolean().optional(),
descriptiveName: z.string(),
});

Expand All @@ -100,7 +91,6 @@ const assetSchema = z.object({
asset: z.object({
id: z.string(),
resourceName: z.string(),
type: z.string(),
youtubeVideoAsset: z
.object({
youtubeVideoId: z.string(),
Expand Down

0 comments on commit 0be2850

Please sign in to comment.