Skip to content

Commit

Permalink
Merge pull request #19 from Design-System-Project/feature/tfr2-125-in…
Browse files Browse the repository at this point in the history
…tegrations-are-being-setup-already-if-the-user-is-new

prevent integrations from showing in not setup accounts
  • Loading branch information
tomasfrancisco authored Aug 29, 2024
2 parents bfc5d09 + 742c23a commit f66fd44
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 54 deletions.
57 changes: 35 additions & 22 deletions packages/api/src/queries/integrations.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import { eq } from '@ds-project/database';
import type { DSContext } from '../types/context';
import { AccountsToProjects, Integrations } from '@ds-project/database/schema';

export const selectGithubIntegration = async ({ ctx }: { ctx: DSContext }) => {
const queryResult = await ctx.database.query.Integrations.findFirst({
columns: {
data: true,
},
where: (integrations) => eq(integrations.type, 'github'),
with: {
project: {
columns: {
id: true,
},
with: {
accountsToProjects: {
columns: {
projectId: true,
},
where: (accountsToProjects) =>
eq(accountsToProjects.accountId, ctx.account.id),
},
},
},
},
});
const [queryResult] = await ctx.database
.select({
data: Integrations.data,
})
.from(Integrations)
.leftJoin(
AccountsToProjects,
eq(AccountsToProjects.projectId, Integrations.projectId)
)
.where(eq(AccountsToProjects.accountId, ctx.account.id))
.limit(1);

if (queryResult?.data?.type === 'github') {
return {
Expand All @@ -34,3 +24,26 @@ export const selectGithubIntegration = async ({ ctx }: { ctx: DSContext }) => {

return undefined;
};

export const selectFigmaIntegration = async ({ ctx }: { ctx: DSContext }) => {
const [queryResult] = await ctx.database
.select({
data: Integrations.data,
})
.from(Integrations)
.leftJoin(
AccountsToProjects,
eq(AccountsToProjects.projectId, Integrations.projectId)
)
.where(eq(AccountsToProjects.accountId, ctx.account.id))
.limit(1);

if (queryResult?.data?.type === 'figma') {
return {
...queryResult,
data: queryResult.data,
};
}

return undefined;
};
37 changes: 5 additions & 32 deletions packages/api/src/router/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { eq } from '@ds-project/database';

import { createTRPCRouter, protectedProcedure } from '../trpc';
import { Resources } from '@ds-project/database/schema';
import { selectGithubIntegration } from '../queries/integrations';
import {
selectFigmaIntegration,
selectGithubIntegration,
} from '../queries/integrations';

export const integrationsRouter = createTRPCRouter({
byId: protectedProcedure
Expand All @@ -20,36 +23,6 @@ export const integrationsRouter = createTRPCRouter({
}),

figma: protectedProcedure.query(async ({ ctx }) => {
const queryResult = await ctx.database.query.Integrations.findFirst({
columns: {
data: true,
},
where: (integrations) => eq(integrations.type, 'figma'),
with: {
project: {
columns: {
id: true,
},
with: {
accountsToProjects: {
columns: {
projectId: true,
},
where: (accountsToProjects) =>
eq(accountsToProjects.accountId, ctx.account.id),
},
},
},
},
});

if (queryResult?.data?.type === 'figma') {
return {
...queryResult,
data: queryResult.data,
};
}

return undefined;
return selectFigmaIntegration({ ctx });
}),
});
2 changes: 2 additions & 0 deletions packages/database/src/schema/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const integrationTypeEnum = pgEnum('integration_type', [
]);
export const integrationType = z.enum(integrationTypeEnum.enumValues);

export type IntegrationType = z.infer<typeof integrationType>;

export const githubIntegrationSchema = z.object({
type: z.literal(integrationType.Enum.github),
installationId: z.number(),
Expand Down

0 comments on commit f66fd44

Please sign in to comment.