Skip to content

Commit

Permalink
Remove query event details api
Browse files Browse the repository at this point in the history
  • Loading branch information
jinalviranii committed Oct 23, 2024
1 parent 753ed78 commit 7cbc9ef
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 57 deletions.
40 changes: 1 addition & 39 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,10 @@ app.get('/fp/auth', passport.authenticate('oauth2', { failureRedirect: '/' }), s
});
res.header['x-company-id'] = companyId;

// Fetch webhook event configuration for an event
const eventData = await fetchEventConfiguration(token.access_token);
const eventIds = eventData.event_configs.map((event) => event.id);

// Subscribe to a specific event
await configureWebhookSubscriber(
token.access_token,
req.query.company_id,
eventIds
);
// Redirect based on company or application ID
const redirectUrl = req.query.application_id
Expand Down Expand Up @@ -215,41 +210,8 @@ function sessionMiddleware(strict) {
};
}

// Function to query event details
async function fetchEventConfiguration(accessToken) {
try {
// Extension webhook configuration. Add/Update this configuration as per need.
const webhookConfig = [
{
event_category: 'company',
event_name: 'product',
event_type: 'delete',
version: '1',
},
];

const requestOptions = {
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(webhookConfig),
};

const response = await fetch(
`${EXTENSION_CLUSTER_URL}/service/common/webhook/v1.0/events/query-event-details`,
requestOptions
);
return response.json(); // Return event data
}
catch (error) {
console.error(`Error while fetching webhook events configuration, Reason: ${error.message}`);
}
}

// Function to configure webhook subscriber
async function configureWebhookSubscriber(accessToken, companyId, eventIds) {
async function configureWebhookSubscriber(accessToken, companyId) {
try {
const requestOptions = {
method: 'POST',
Expand Down
19 changes: 1 addition & 18 deletions src/tests/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ describe('Custom Next.js server', () => {
});

it('GET /fp/auth: Should return access token and subscribe to webhook event', async () => {
// Mock the fetch request for queryEventDetails
fetch.mockImplementationOnce(async (url) => {
if (url.includes('/query-event-details')) {
return {
json: async () => ({
event_configs: [{ id: 'mockedEventId1' }],
}),
};
}
return Promise.reject(new Error('Unknown endpoint'));
});

// Mock the response for the /subscriber endpoint
fetch.mockImplementationOnce(async (url) => {
if (url.includes('/subscriber')) {
Expand All @@ -84,14 +72,9 @@ describe('Custom Next.js server', () => {
.get(`/fp/auth?company_id=123`)
.set('cookie', `${SESSION_COOKIE_NAME}_123=${cookie}`)
.send();
expect(fetch).toHaveBeenCalledTimes(2);
expect(fetch).toHaveBeenCalledTimes(1);

expect(fetch).toHaveBeenNthCalledWith(1,
`${process.env.EXTENSION_CLUSTER_URL}/service/common/webhook/v1.0/events/query-event-details`,
expect.any(Object)
);

expect(fetch).toHaveBeenNthCalledWith(2,
`${process.env.EXTENSION_CLUSTER_URL}/service/platform/webhook/v2.0/company/123/subscriber`,
expect.any(Object)
);
Expand Down

0 comments on commit 7cbc9ef

Please sign in to comment.