Skip to content

Commit

Permalink
Merge pull request #277 from middlewarehq/GROW-1477
Browse files Browse the repository at this point in the history
Update integrationsLinkedAtMap in org API response
  • Loading branch information
shivam-bit authored May 6, 2024
2 parents 3422b9c + 4a5bf52 commit ebccf2b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
6 changes: 5 additions & 1 deletion web-server/libdefs/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare type Org = {
domain: string;
onboarding_state: string[];
integrations: Partial<IntegrationsMap>;
integrationsLinkedAtMap: Partial<IntegrationsLinkedAtMap>;
};

declare type ONBOARDING_STEP =
Expand Down Expand Up @@ -43,7 +44,10 @@ declare type IdentityMap = Record<
>;

declare type IntegrationsMap = Record<'github' | 'gitlab' | 'bitbucket', true>;

declare type IntegrationsLinkedAtMap = Record<
'github' | 'gitlab' | 'bitbucket',
DateString
>;
declare type User = {
id: string;
created_at: Date;
Expand Down
35 changes: 24 additions & 11 deletions web-server/pages/api/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextApiResponse } from 'next/types';

import { getOnBoardingState } from '@/api/resources/orgs/[org_id]/onboarding';
import { Endpoint, nullSchema } from '@/api-helpers/global';
import { Table } from '@/constants/db';
import { Row, Table } from '@/constants/db';
import { db, getFirstRow } from '@/utils/db';

const endpoint = new Endpoint(nullSchema);
Expand Down Expand Up @@ -30,14 +30,18 @@ export const delUserIdCookie = (res: NextApiResponse) => {
};

endpoint.handle.GET(nullSchema, async (_req, res) => {
const [orgDetails, integrations] = await Promise.all([
getOrgDetails(),
getOrgIntegrations()
]);
const [orgDetails, { integrations, integrationsLinkedAtMap }] =
await Promise.all([getOrgDetails(), getOrgIntegrations()]);

const onboardingState = await getOnBoardingState(orgDetails.id);
res.send({
org: { ...orgDetails, ...onboardingState, integrations } || {}
org:
{
...orgDetails,
...onboardingState,
integrations,
integrationsLinkedAtMap
} || {}
});
});

Expand All @@ -49,14 +53,23 @@ const getOrgDetails = async () => {

const getOrgIntegrations = async () => {
return db(Table.Integration)
.select('name')
.select('*')
.whereNotNull('access_token_enc_chunks')
.then((rows) =>
rows
.then((rows) => {
const integrations = rows
.map((row) => row.name)
.reduce(
(map: IntegrationsMap, name: string) => ({ ...map, [name]: true }),
{} as IntegrationsMap
)
);
);
const integrationsLinkedAtMap = rows.reduce(
(map: IntegrationsLinkedAtMap, r: Row<'Integration'>) => ({
...map,
[r.name]: r.created_at
}),
{} as IntegrationsLinkedAtMap
);

return { integrations, integrationsLinkedAtMap };
});
};
10 changes: 4 additions & 6 deletions web-server/src/contexts/ThirdPartyAuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface AuthContextValue extends AuthState {
orgId: string | null;
role: UserRole;
integrations: User['integrations'];
integrationsLinkedAtMap: Org['integrationsLinkedAtMap'];
onboardingState: OnboardingStep[];
integrationSet: Set<IntegrationGroup>;
}
Expand All @@ -27,6 +28,7 @@ export const AuthContext = createContext<AuthContextValue>({
orgId: null,
role: UserRole.MOM,
integrations: {},
integrationsLinkedAtMap: {},
onboardingState: [],
integrationSet: new Set()
});
Expand Down Expand Up @@ -70,11 +72,6 @@ export const AuthProvider: FC = (props) => {
if (!isMounted()) return;
const org = session?.org;
if (org) {
// @ts-ignore
// window.FreshworksWidget?.('identify', 'ticketForm', {
// name: identifyConfig.name,
// email: identifyConfig.email
// });
dispatch(
authSlice.actions.init({
isAuthenticated: true,
Expand Down Expand Up @@ -116,8 +113,9 @@ export const AuthProvider: FC = (props) => {
orgId: state.org?.id,
role,
integrations,
integrationsLinkedAtMap: state.org?.integrationsLinkedAtMap || {},
integrationSet,
onboardingState: state.org?.onboarding_state || []
onboardingState: (state.org?.onboarding_state as OnboardingStep[]) || []
}}
>
{children}
Expand Down

0 comments on commit ebccf2b

Please sign in to comment.