server.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. require('dotenv').config();
  2. const app = require('fastify')({ logger: false });
  3. const axios = require('axios');
  4. const { getDb } = require('./utils/MongoDBConnector');
  5. const RabbitMQProducer = require('./utils/RabbitMQProducer');
  6. const GRAPH_API = 'https://graph.facebook.com/v22.0';
  7. // The public base URL of this app (used for OAuth redirect_uri)
  8. const APP_BASE_URL = process.env.APP_BASE_URL || 'http://localhost:8081';
  9. // ─── CORS ────────────────────────────────────────────────────────────────────
  10. app.addHook('onSend', async (request, reply) => {
  11. reply.header('Access-Control-Allow-Origin', '*');
  12. reply.header('Access-Control-Allow-Methods', 'GET,POST,DELETE,OPTIONS');
  13. reply.header('Access-Control-Allow-Headers', 'Content-Type');
  14. });
  15. app.options('*', async (request, reply) => {
  16. reply.code(204).send();
  17. });
  18. // ─── Helpers ─────────────────────────────────────────────────────────────────
  19. async function getCredentials(id) {
  20. const db = await getDb();
  21. return db.collection('platform_credentials').findOne({ _id: id });
  22. }
  23. async function setCredentials(id, data) {
  24. const db = await getDb();
  25. await db.collection('platform_credentials').updateOne(
  26. { _id: id },
  27. { $set: { _id: id, ...data, updatedAt: new Date() } },
  28. { upsert: true }
  29. );
  30. }
  31. async function deleteCredentials(id) {
  32. const db = await getDb();
  33. await db.collection('platform_credentials').deleteOne({ _id: id });
  34. }
  35. // ─── Legacy post route ────────────────────────────────────────────────────────
  36. let rabbitMQProducer = new RabbitMQProducer();
  37. app.post('/', async (request, reply) => {
  38. try {
  39. await rabbitMQProducer.sendMessage('formatter', request.body.message);
  40. reply.send({ status: 'ok' });
  41. } catch (error) {
  42. console.error('Error handling POST request:', error);
  43. reply.status(500).send({ error: 'Internal Server Error' });
  44. }
  45. });
  46. // ─── Meta App Credentials ────────────────────────────────────────────────────
  47. // Save Facebook App ID + Secret (entered by user in Settings UI)
  48. app.post('/credentials/meta-app', async (request, reply) => {
  49. const { appId, appSecret } = request.body || {};
  50. if (!appId || !appSecret) {
  51. return reply.code(400).send({ error: 'appId and appSecret are required' });
  52. }
  53. await setCredentials('meta_app', { appId, appSecret });
  54. return { success: true };
  55. });
  56. // Get Meta App config (secret is masked for UI display)
  57. app.get('/credentials/meta-app', async () => {
  58. const cred = await getCredentials('meta_app');
  59. if (!cred) return { configured: false };
  60. return { configured: true, appId: cred.appId, appSecretHint: `****${cred.appSecret.slice(-4)}` };
  61. });
  62. // ─── Meta OAuth Flow ──────────────────────────────────────────────────────────
  63. // Return the Facebook OAuth URL to redirect the user to
  64. app.get('/auth/meta/init', async (request, reply) => {
  65. const cred = await getCredentials('meta_app');
  66. if (!cred?.appId) {
  67. return reply.code(400).send({ error: 'Save your Facebook App ID and Secret first' });
  68. }
  69. const redirectUri = `${APP_BASE_URL}/api/auth/meta/callback`;
  70. const scopes = [
  71. 'pages_manage_posts',
  72. 'pages_read_engagement',
  73. 'instagram_basic',
  74. 'instagram_content_publish',
  75. 'instagram_manage_insights',
  76. ].join(',');
  77. const url = `https://www.facebook.com/v22.0/dialog/oauth?client_id=${cred.appId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scopes}&response_type=code`;
  78. return { url };
  79. });
  80. // OAuth callback — Facebook redirects here after user authorises
  81. app.get('/auth/meta/callback', async (request, reply) => {
  82. const { code, error: oauthError } = request.query;
  83. if (oauthError) {
  84. return reply.redirect(`${APP_BASE_URL}/settings?meta_error=${encodeURIComponent(oauthError)}`);
  85. }
  86. if (!code) {
  87. return reply.redirect(`${APP_BASE_URL}/settings?meta_error=no_code`);
  88. }
  89. try {
  90. const appCred = await getCredentials('meta_app');
  91. if (!appCred?.appId) throw new Error('App credentials not configured');
  92. const redirectUri = `${APP_BASE_URL}/api/auth/meta/callback`;
  93. // Exchange code for short-lived token
  94. const shortRes = await axios.get(`${GRAPH_API}/oauth/access_token`, {
  95. params: {
  96. client_id: appCred.appId,
  97. client_secret: appCred.appSecret,
  98. redirect_uri: redirectUri,
  99. code,
  100. },
  101. });
  102. // Upgrade to long-lived user token (~60 days)
  103. const longRes = await axios.get(`${GRAPH_API}/oauth/access_token`, {
  104. params: {
  105. grant_type: 'fb_exchange_token',
  106. client_id: appCred.appId,
  107. client_secret: appCred.appSecret,
  108. fb_exchange_token: shortRes.data.access_token,
  109. },
  110. });
  111. const userToken = longRes.data.access_token;
  112. // Fetch all managed Facebook Pages
  113. const pagesRes = await axios.get(`${GRAPH_API}/me/accounts`, {
  114. params: { access_token: userToken, fields: 'id,name,access_token,picture' },
  115. });
  116. const pages = [];
  117. const igAccounts = [];
  118. for (const page of pagesRes.data.data || []) {
  119. pages.push({
  120. id: page.id,
  121. name: page.name,
  122. accessToken: page.access_token,
  123. picture: page.picture?.data?.url || null,
  124. selected: false,
  125. });
  126. // Check for linked Instagram Business Account
  127. try {
  128. const igRes = await axios.get(`${GRAPH_API}/${page.id}`, {
  129. params: {
  130. fields: 'instagram_business_account',
  131. access_token: page.access_token,
  132. },
  133. });
  134. if (igRes.data.instagram_business_account?.id) {
  135. const igId = igRes.data.instagram_business_account.id;
  136. // Fetch IG account details
  137. const igProfile = await axios.get(`${GRAPH_API}/${igId}`, {
  138. params: {
  139. fields: 'id,username,name,profile_picture_url',
  140. access_token: userToken,
  141. },
  142. });
  143. igAccounts.push({
  144. id: igId,
  145. username: igProfile.data.username || igProfile.data.name,
  146. name: igProfile.data.name,
  147. avatar: igProfile.data.profile_picture_url || null,
  148. accessToken: userToken,
  149. pageId: page.id,
  150. selected: false,
  151. });
  152. }
  153. } catch (_) {
  154. // Page has no linked Instagram account — skip
  155. }
  156. }
  157. // Store discovery results for the UI to pick from
  158. await setCredentials('meta_discovery', { pages, igAccounts, discoveredAt: new Date() });
  159. reply.redirect(`${APP_BASE_URL}/settings?meta_discovery=1`);
  160. } catch (err) {
  161. console.error('[Gateway] Meta OAuth error:', err.response?.data || err.message);
  162. reply.redirect(`${APP_BASE_URL}/settings?meta_error=${encodeURIComponent(err.message)}`);
  163. }
  164. });
  165. // Return pending discovery results so the UI can render the page picker
  166. app.get('/auth/meta/discovered', async () => {
  167. const discovery = await getCredentials('meta_discovery');
  168. if (!discovery) return { pages: [], igAccounts: [] };
  169. return { pages: discovery.pages || [], igAccounts: discovery.igAccounts || [] };
  170. });
  171. // User has chosen which pages/accounts to connect
  172. app.post('/auth/meta/save', async (request, reply) => {
  173. const { selectedPageIds = [], selectedIgAccountIds = [] } = request.body || {};
  174. const discovery = await getCredentials('meta_discovery');
  175. if (!discovery) return reply.code(400).send({ error: 'No discovery data found — reconnect via OAuth' });
  176. const fbPages = (discovery.pages || []).map((p) => ({
  177. ...p,
  178. selected: selectedPageIds.includes(p.id),
  179. }));
  180. const igAccounts = (discovery.igAccounts || []).map((a) => ({
  181. ...a,
  182. selected: selectedIgAccountIds.includes(a.id),
  183. }));
  184. await setCredentials('facebook', { pages: fbPages });
  185. await setCredentials('instagram', { accounts: igAccounts });
  186. await deleteCredentials('meta_discovery');
  187. return { success: true, facebookPages: fbPages.filter((p) => p.selected).length, instagramAccounts: igAccounts.filter((a) => a.selected).length };
  188. });
  189. // Disconnect all Meta platforms
  190. app.delete('/credentials/meta', async () => {
  191. await deleteCredentials('facebook');
  192. await deleteCredentials('instagram');
  193. await deleteCredentials('meta_discovery');
  194. return { success: true };
  195. });
  196. // ─── Credential Status ────────────────────────────────────────────────────────
  197. // Aggregate connection status for all DB-managed platforms
  198. app.get('/credentials', async () => {
  199. const [metaApp, fb, ig] = await Promise.all([
  200. getCredentials('meta_app'),
  201. getCredentials('facebook'),
  202. getCredentials('instagram'),
  203. ]);
  204. const fbPages = (fb?.pages || []).filter((p) => p.selected);
  205. const igAccounts = (ig?.accounts || []).filter((a) => a.selected);
  206. return {
  207. metaApp: { configured: !!(metaApp?.appId) },
  208. facebook: {
  209. connected: fbPages.length > 0,
  210. pages: fbPages.map(({ id, name, picture }) => ({ id, name, picture })),
  211. },
  212. instagram: {
  213. connected: igAccounts.length > 0,
  214. accounts: igAccounts.map(({ id, username, avatar }) => ({ id, username, avatar })),
  215. },
  216. };
  217. });
  218. module.exports = app;