Skip to content

Commit ebccf2b

Browse files
authored
Merge pull request #277 from middlewarehq/GROW-1477
Update integrationsLinkedAtMap in org API response
2 parents 3422b9c + 4a5bf52 commit ebccf2b

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

web-server/libdefs/ambient.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ declare type Org = {
1616
domain: string;
1717
onboarding_state: string[];
1818
integrations: Partial<IntegrationsMap>;
19+
integrationsLinkedAtMap: Partial<IntegrationsLinkedAtMap>;
1920
};
2021

2122
declare type ONBOARDING_STEP =
@@ -43,7 +44,10 @@ declare type IdentityMap = Record<
4344
>;
4445

4546
declare type IntegrationsMap = Record<'github' | 'gitlab' | 'bitbucket', true>;
46-
47+
declare type IntegrationsLinkedAtMap = Record<
48+
'github' | 'gitlab' | 'bitbucket',
49+
DateString
50+
>;
4751
declare type User = {
4852
id: string;
4953
created_at: Date;

web-server/pages/api/auth/session.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NextApiResponse } from 'next/types';
22

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

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

3232
endpoint.handle.GET(nullSchema, async (_req, res) => {
33-
const [orgDetails, integrations] = await Promise.all([
34-
getOrgDetails(),
35-
getOrgIntegrations()
36-
]);
33+
const [orgDetails, { integrations, integrationsLinkedAtMap }] =
34+
await Promise.all([getOrgDetails(), getOrgIntegrations()]);
3735

3836
const onboardingState = await getOnBoardingState(orgDetails.id);
3937
res.send({
40-
org: { ...orgDetails, ...onboardingState, integrations } || {}
38+
org:
39+
{
40+
...orgDetails,
41+
...onboardingState,
42+
integrations,
43+
integrationsLinkedAtMap
44+
} || {}
4145
});
4246
});
4347

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

5054
const getOrgIntegrations = async () => {
5155
return db(Table.Integration)
52-
.select('name')
56+
.select('*')
5357
.whereNotNull('access_token_enc_chunks')
54-
.then((rows) =>
55-
rows
58+
.then((rows) => {
59+
const integrations = rows
5660
.map((row) => row.name)
5761
.reduce(
5862
(map: IntegrationsMap, name: string) => ({ ...map, [name]: true }),
5963
{} as IntegrationsMap
60-
)
61-
);
64+
);
65+
const integrationsLinkedAtMap = rows.reduce(
66+
(map: IntegrationsLinkedAtMap, r: Row<'Integration'>) => ({
67+
...map,
68+
[r.name]: r.created_at
69+
}),
70+
{} as IntegrationsLinkedAtMap
71+
);
72+
73+
return { integrations, integrationsLinkedAtMap };
74+
});
6275
};

web-server/src/contexts/ThirdPartyAuthContext.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface AuthContextValue extends AuthState {
1818
orgId: string | null;
1919
role: UserRole;
2020
integrations: User['integrations'];
21+
integrationsLinkedAtMap: Org['integrationsLinkedAtMap'];
2122
onboardingState: OnboardingStep[];
2223
integrationSet: Set<IntegrationGroup>;
2324
}
@@ -27,6 +28,7 @@ export const AuthContext = createContext<AuthContextValue>({
2728
orgId: null,
2829
role: UserRole.MOM,
2930
integrations: {},
31+
integrationsLinkedAtMap: {},
3032
onboardingState: [],
3133
integrationSet: new Set()
3234
});
@@ -70,11 +72,6 @@ export const AuthProvider: FC = (props) => {
7072
if (!isMounted()) return;
7173
const org = session?.org;
7274
if (org) {
73-
// @ts-ignore
74-
// window.FreshworksWidget?.('identify', 'ticketForm', {
75-
// name: identifyConfig.name,
76-
// email: identifyConfig.email
77-
// });
7875
dispatch(
7976
authSlice.actions.init({
8077
isAuthenticated: true,
@@ -116,8 +113,9 @@ export const AuthProvider: FC = (props) => {
116113
orgId: state.org?.id,
117114
role,
118115
integrations,
116+
integrationsLinkedAtMap: state.org?.integrationsLinkedAtMap || {},
119117
integrationSet,
120-
onboardingState: state.org?.onboarding_state || []
118+
onboardingState: (state.org?.onboarding_state as OnboardingStep[]) || []
121119
}}
122120
>
123121
{children}

0 commit comments

Comments
 (0)