-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
683 lines (585 loc) · 24.7 KB
/
server.js
File metadata and controls
683 lines (585 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const path = require("path");
const sqlite3 = require('sqlite3').verbose();
const serveStatic = require("serve-static");
const { readFileSync } = require('fs');
const { setupFdk } = require("@gofynd/fdk-extension-javascript/express");
const { SQLiteStorage } = require("@gofynd/fdk-extension-javascript/express/storage");
const axios = require('axios');
const SecretsModel = require('./models/secrets.model');
const AdvertiseModel = require('./models/advertise.model');
const { isEmpty } = require('lodash');
const sqliteInstance = new sqlite3.Database('session_storage.db');
const productRouter = express.Router();
console.log("first", process.env.EXTENSION_BASE_URL)
const fdkExtension = setupFdk({
api_key: process.env.EXTENSION_API_KEY,
api_secret: process.env.EXTENSION_API_SECRET,
base_url: process.env.EXTENSION_BASE_URL,
cluster: process.env.FP_API_DOMAIN,
callbacks: {
auth: async (req) => {
// Write you code here to return initial launch url after auth process complete
if (req.query.application_id)
return `${req.extension.base_url}/company/${req.query['company_id']}/application/${req.query.application_id}`;
else
return `${req.extension.base_url}/company/${req.query['company_id']}`;
},
uninstall: async (req) => {
// Write your code here to cleanup data related to extension
// If task is time taking then process it async on other process.
}
},
storage: new SQLiteStorage(sqliteInstance, "exapmple-fynd-platform-extension"), // add your prefix
access_mode: "online",
webhook_config: {
api_path: "/api/webhook-events",
notification_email: "pritigediya@fynd-extenal.com",
event_map: {
"company/product/delete": {
"handler": (eventName) => { console.log(eventName) },
"version": '1'
}
}
},
});
const STATIC_PATH = process.env.NODE_ENV === 'production'
? path.join(process.cwd(), 'frontend', 'public', 'dist')
: path.join(process.cwd(), 'frontend');
const app = express();
const platformApiRoutes = fdkExtension.platformApiRoutes;
// Middleware to parse cookies with a secret key
app.use(cookieParser("ext.session"));
// Middleware to parse JSON bodies with a size limit of 2mb
app.use(bodyParser.json({
limit: '2mb'
}));
// Serve static files from the React dist directory
app.use(serveStatic(STATIC_PATH, { index: false }));
// FDK extension handler and API routes (extension launch routes)
app.use("/", fdkExtension.fdkHandler);
// Route to handle webhook events and process it.
app.post('/api/webhook-events', async function (req, res) {
try {
console.log(`Webhook Event: ${req.body.event} received`)
await fdkExtension.webhookRegistry.processWebhook(req);
return res.status(200).json({ "success": true });
} catch (err) {
console.log(`Error Processing ${req.body.event} Webhook`);
return res.status(500).json({ "success": false });
}
})
// // // // // // // // //
app.post('/api/auth/login', (req, res) => {
console.log("coming")
// const redirectUri = 'https://abc.com/callback'
const redirectUri = `${process.env.EXTENSION_BASE_URL}/company`
const { company_id, application_id, client_id, client_secret } = req.body
const states = { company_id, application_id, client_id, client_secret }
const scopes = [
"instagram_basic",
"ads_management",
"business_management",
"pages_read_engagement",
"pages_manage_ads"
].join(',');
const authUrl = `https://www.facebook.com/v18.0/dialog/oauth?client_id=${client_id}&redirect_uri=${redirectUri}&scope=${scopes}&response_type=code&state=${JSON.stringify(states)}`;
return res.status(200).json({ "success": true, authUrl });
})
app.post('/api/generate-captions', async (req, res) => {
const { description } = req.body
const promptArray = [
"Write a catchy and engaging Instagram caption announcing a limited-time storefront sale. Make it exciting and include a strong call to action.",
"Generate a playful and witty Instagram caption for a storefront sale that encourages followers to visit and shop before the deals run out.",
"Create a short and punchy Instagram caption for a storefront sale, incorporating urgency and exclusivity to drive customers to take action.",
"Write a warm and inviting Instagram caption for a storefront sale that highlights community, great deals, and the joy of shopping in person.",
"Generate an Instagram caption for a storefront sale that uses emojis and trendy language to appeal to a younger audience and drive engagement.",
];
const choice = Math.floor(Math.random() * 5);
try {
const captionResponse = await axios.post("https://api.openai.com/v1/chat/completions", {
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: description ? `create caption from this product description html ${description}, muts be a normal string no "" or '' is needed i need only text` : promptArray[choice] }],
max_tokens: 50,
}, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
});
const caption = captionResponse.data.choices[0].message.content;
const hashtagResponse = await axios.post("https://api.openai.com/v1/chat/completions", {
model: "gpt-3.5-turbo",
messages: [{
role: "system",
content: `
Taking this caption in context ${caption}
You are a social media expert. Always respond with valid JSON in the following strict format:
[
"tag1",
"tag2",
"tag3"
]
The response must be **an array of exactly three strings** and nothing else. No extra text, objects, or formatting—only the required array of three strings. This is extremely important.`
},],
max_tokens: 50,
}, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
});
const hashtags = hashtagResponse.data.choices[0].message.content;
return res.status(200).send({ caption, hashtags });
// const data = await caption.json();
// return data.choices[0].message.content;
// return res.status(200).send({ caption: data.choices[0].message.content });
} catch (error) {
console.log({ error });
}
});
app.get('/api/instagram/auth', async (req, res) => {
try {
console.log({ query: req.query });
const { code,
company_id: companyId,
application_id: applicationId,
client_id: clientId,
client_secret: clientSecret } = req.query;
// Get access token
const tokenResponse = await axios.get('https://graph.facebook.com/v18.0/oauth/access_token', {
params: {
client_id: clientId,
redirect_uri: `${process.env.EXTENSION_BASE_URL}/company`,
client_secret: clientSecret,
code: code
}
});
const accessToken = tokenResponse.data.access_token;
// Increase TTL of token
await axios.get('https://graph.facebook.com/v18.0/oauth/access_token', {
params: {
grant_type: 'fb_exchange_token',
client_id: clientId,
client_secret: clientSecret,
fb_exchange_token: accessToken
}
});
// Get Instagram accounts using the access token
const accountsResponse = await fetch(`https://graph.facebook.com/v22.0/me/accounts?access_token=${accessToken}`);
const accountsData = await accountsResponse.json();
console.log(JSON.stringify(accountsData));
// Fetch Ad Account ID
const adAccountResponse = await fetch(
`https://graph.facebook.com/v18.0/me/adaccounts?fields=account_id,name&access_token=${accessToken}`
);
const adAccountData = await adAccountResponse.json();
// For each account, fetch the Instagram Business Account
const accountsWithInstagram = await Promise.all(accountsData.data.map(async (account) => {
const instagramResponse = await fetch(`https://graph.facebook.com/v22.0/${account.id}?fields=instagram_business_account&access_token=${accessToken}`);
const instagramData = await instagramResponse.json(); // store pageId = account.id in secrete table in DB
return {
...account,
instagram_business_account: instagramData.instagram_business_account || null
};
}));
// Store in DB
const instagramAccountData = accountsWithInstagram[0];
const newSecret = await SecretsModel.create({
companyId,
applicationId,
instagramBusinessId: instagramAccountData.instagram_business_account.id,
adAccountId: adAccountData.data[0].account_id,
adAccountName: adAccountData.data[0].name,
accessToken,
clientId,
clientSecret
});
console.log({ newSecret });
if (newSecret.error) {
if (newSecret.message.includes('already exists')) {
return res.status(409).json({ error: newSecret.message });
}
}
return res.status(200).json({
...instagramAccountData,
companyId,
applicationId,
redirectUrl: `${process.env.FP_PLATFORM_DOMAIN}/company/${companyId}/application/${applicationId}/extensions/${process.env.EXTENSION_API_KEY}`
});
} catch (error) {
console.error('Error:', error.response?.data || error);
res.status(500).json({ message: 'Failed to process request', error });
}
});
app.get('/api/secrets', async (req, res) => {
try {
const { company_id, application_id } = req.query;
console.log('/api/secrets', { company_id, application_id });
const secrets = await SecretsModel.getByCompanyAndAppId({ companyId: company_id, applicationId: application_id })
console.log({ secrets });
res.status(200).json({
isSellerAuthentiated: isEmpty(secrets) ? false : true,
platformDomain: process.env.FP_PLATFORM_DOMAIN,
extensionId: process.env.EXTENSION_API_KEY
});
} catch (error) {
console.error('Error fetching secrets:', error);
res.status(500).json({ success: false, message: 'Failed to fetch secrets', error });
}
});
app.get('/api/instagram/account', async (req, res) => {
try {
const { company_id, application_id } = req.query;
// Get credentials from secrets
const secrets = await SecretsModel.getByCompanyAndAppId({
companyId: company_id,
applicationId: application_id
});
const { accessToken, instagramBusinessId } = secrets;
// Fetch Instagram Business Account details
const response = await axios.get(
`https://graph.facebook.com/v18.0/${instagramBusinessId}`,
{
params: {
fields: 'username,profile_picture_url,name,biography,website,followers_count,media_count',
access_token: accessToken
}
}
);
return res.status(200).json({
success: true,
data: response.data
});
} catch (error) {
console.error('Error fetching Instagram account:', error.response?.data || error);
return res.status(500).json({
success: false,
message: 'Failed to fetch Instagram account details',
error: error.message
});
}
});
app.post('/api/instagram/post', async (req, res) => {
try {
const {
imageUrl,
caption,
company_id,
application_id,
createAd = false,
productId,
adConfig
// = {
// cta_type: 'SHOP_NOW',
// daily_budget: 8700, // in cents ($5)
// campaign_name: 'FYND_AD_CAMPAIGN',
// website_url: 'https://codepulse.fynd.io/product/m6qeb3cr_co-13438877',
// targeting: {
// age_min: 18,
// age_max: 65,
// countries: ['IN']
// }
// }
} = req.body;
// Validate inputs
if (!imageUrl || !caption || !company_id) {
return res.status(400).json({
success: false,
message: 'Image URL, caption and company_id are required'
});
}
// Get credentials from secrets
const secrets = await SecretsModel.getByCompanyAndAppId({
companyId: company_id,
applicationId: application_id
});
const { accessToken, instagramBusinessId, adAccountId, id: secretsId } = secrets;
// Create media container
const containerResponse = await axios.post(
`https://graph.facebook.com/v22.0/${instagramBusinessId}/media`,
null,
{
params: {
image_url: imageUrl,
caption: caption,
access_token: accessToken
}
}
);
if (!containerResponse.data.id) {
throw new Error('Failed to create media container');
}
// Publish the container
const publishResponse = await axios.post(
`https://graph.facebook.com/v22.0/${instagramBusinessId}/media_publish`,
null,
{
params: {
creation_id: containerResponse.data.id,
access_token: accessToken
}
}
);
const postId = publishResponse.data.id;
// Get post permalink
const mediaResponse = await axios.get(
`https://graph.facebook.com/v22.0/${postId}`,
{
params: {
fields: 'permalink',
access_token: accessToken
}
}
);
let adResponse = null;
// Create ad if requested
if (createAd && postId) {
try {
// Create Campaign
console.log('Creating Campaign with params:', {
name: adConfig.campaign_name,
objective: 'OUTCOME_SALES',
status: 'ACTIVE',
special_ad_categories: '[]',
access_token: accessToken
});
const campaignResponse = await axios.post(
`https://graph.facebook.com/v22.0/act_${adAccountId}/campaigns`,
null,
{
params: {
name: adConfig.campaign_name,
objective: 'OUTCOME_SALES',
status: 'ACTIVE',
special_ad_categories: '[]',
access_token: accessToken
}
}
);
const campaignId = campaignResponse.data.id;
// Create Ad Set
console.log('Creating Ad Set with params:', {
name: `${adConfig.campaign_name} Ad Set`,
campaign_id: campaignId,
daily_budget: adConfig.daily_budget,
billing_event: 'IMPRESSIONS',
optimization_goal: 'LINK_CLICKS',
bid_strategy: 'LOWEST_COST_WITHOUT_CAP',
targeting: JSON.stringify({
age_min: adConfig.targeting.age_min,
age_max: adConfig.targeting.age_max,
genders: [1, 2],
geo_locations: {
countries: adConfig.targeting.countries
}
}),
status: 'ACTIVE',
access_token: accessToken
});
const adSetResponse = await axios.post(
`https://graph.facebook.com/v22.0/act_${adAccountId}/adsets`,
null,
{
params: {
name: `${adConfig.campaign_name} Ad Set`,
campaign_id: campaignId,
daily_budget: adConfig.daily_budget,
billing_event: 'IMPRESSIONS',
optimization_goal: 'LINK_CLICKS',
bid_strategy: 'LOWEST_COST_WITHOUT_CAP',
targeting: JSON.stringify({
publisher_platforms: ["instagram"],
instagram_positions: ["stream", "explore", "explore_home"],
age_min: adConfig.targeting.age_min,
age_max: adConfig.targeting.age_max,
genders: [1, 2],
geo_locations: {
countries: adConfig.targeting.countries
}
}),
status: 'ACTIVE',
access_token: accessToken
}
}
);
const adSetId = adSetResponse.data.id;
// Create Ad Creative
console.log('Creating Ad Creative with params:', {
object_story_id: postId,
call_to_action_type: adConfig.cta_type,
link_data: {
call_to_action: {
type: adConfig.cta_type,
value: {
link: adConfig.website_url
}
}
},
access_token: accessToken
});
const creativeResponse = await axios.post(
`https://graph.facebook.com/v22.0/act_${adAccountId}/adcreatives`,
null,
{
params: {
object_id: '606013975918017',
instagram_user_id: instagramBusinessId,
source_instagram_media_id: postId,
call_to_action: {
value: {
link: adConfig.website_url
},
type: adConfig.cta_type
},
// call_to_action_type: adConfig.cta_type,
// link_data: {
// call_to_action: {
// type: adConfig.cta_type,
// value: {
// link: adConfig.website_url
// }
// }
// },
access_token: accessToken
}
}
);
const creativeId = creativeResponse.data.id;
// Create Ad
console.log('Creating Ad with params:', {
name: `${adConfig.campaign_name} Ad`,
adset_id: adSetId,
creative: { creative_id: creativeId },
status: 'ACTIVE',
access_token: accessToken
});
adResponse = await axios.post(
`https://graph.facebook.com/v22.0/act_${adAccountId}/ads`,
null,
{
params: {
name: `${adConfig.campaign_name} Ad`,
adset_id: adSetId,
creative: { creative_id: creativeId },
status: 'ACTIVE',
access_token: accessToken
}
}
);
console.log("adResponse", adResponse?.data);
const createAdvertise = await AdvertiseModel.create({
secretsId: secretsId,
productId,
ctaType: adConfig.cta_type,
budget: adConfig.daily_budget,
minAge: adConfig.targeting.age_min,
maxAge: adConfig.targeting.age_max,
campaignName: adConfig.campaign_name,
websiteUrl: adConfig.website_url,
postId,
campaignId,
adsetId: adSetId,
adCreativeId: creativeId,
permaLink: mediaResponse.data.permalink,
adId: adResponse?.data?.id,
})
console.log("Added in Advertise db", { createAdvertise });
return res.status(200).json({
success: true,
message: 'Posted successfully to Instagram',
post_id: postId,
permalink: mediaResponse.data.permalink,
ad_data: adResponse ? {
campaign_id: campaignId,
ad_set_id: adSetId,
creative_id: creativeId,
ad_id: adResponse?.data?.id
} : null
});
} catch (adError) {
console.error('Ad creation failed:', adError.response?.data || adError);
return res.status(402).json({
success: false,
message: 'Operation failed',
error: adError.response?.data.error
});
}
}
} catch (error) {
console.error('Error:', error.response?.data || error);
return res.status(500).json({
success: false,
message: 'Operation failed',
error: error.message
});
}
});
productRouter.get('/:product_slug/application/:application_id', async (req, res) => {
try {
const { application_id, product_slug } = req.params;
const applicationClient = req?.platformClient?.application(application_id);
const resp = await applicationClient?.configuration?.getDomains({
companyId: req.headers['x-company-id'],
applicationId: application_id,
})
const pdpURL = `https://${resp?.domains?.[0]?.name}/product/${product_slug}`
return res.json({
pdpURL
});
} catch (error) {
console.error('Error fetching Instagram account:', error.response?.data || error);
return res.status(500).json({
success: false,
message: 'Failed to fetch Instagram account details',
error: error.message
});
}
});
// // // // // // // // // //
productRouter.get('/', async function view(req, res, next) {
try {
const {
platformClient
} = req;
const data = await platformClient.catalog.getProducts()
return res.json(data);
} catch (err) {
next(err);
}
});
// Get products list for application
productRouter.get('/application/:application_id', async function view(req, res, next) {
try {
const {
platformClient
} = req;
const { application_id } = req.params;
const data = await platformClient.application(application_id).catalog.getAppProducts()
return res.json(data);
} catch (err) {
next(err);
}
});
// FDK extension api route which has auth middleware and FDK client instance attached to it.
platformApiRoutes.use('/products', productRouter);
// If you are adding routes outside of the /api path,
// remember to also add a proxy rule for them in /frontend/vite.config.js
app.use('/api', platformApiRoutes);
const healthRouter = express.Router();
healthRouter.get('/', (req, res, next) => {
res.json({
"ok": "ok"
});
});
app.use("/", healthRouter);
// Serve the React app for all other routes
app.get('*', (req, res) => {
return res
.status(200)
.set("Content-Type", "text/html")
.send(readFileSync(path.join(STATIC_PATH, "index.html")));
});
module.exports = app;