* Add db schema * Add `CredentialRepository.findByTeamIdAndSlugs` * Add enabled app slugs for attribute syncing * Create repository for `IntegrationAttributeSync` * Create zod schemas * Create `AttributeSyncUserRuleOutputMapper` * Create `IntegrationAttributeSyncService` * Create DI contianer * Create trpc endpoints * Create page * Include team name in `CredentialRepository.findByTeamIdAndSlugs` * Update schema and relations * Update types and schemas * Add more methods to IntegrationAttributeSyncRepository * Add more services to `IntegrationAttributeSyncService` - getById - Init updateIncludeRulesAndMappings * Refactor `getTeams.handler` to use repository * Create `createAttributeSync` trpc endpoint * Create `updateAttributeSync` trpc endpoint * Add router to trpc * Create attribute sync child components * Pass custom actions to `FormCard` * Create `IntegrationAttributeSyncCard` * Pass inital props via server side * Fix prop * Only refetch on mutation * Fixes * Add form error when duplicate field and attribute combo * Add `updateTransactionWithRUleAndMappings` logic * Adjust zod schemas * Service add `updateIncludeRulesAndMappings` * Pass orgId from server to component * Rename types * Add deleteById method to repository * Add name to integrationAttributeSync * Add deleteById method to service * Rename method * Add deleteAttributeSync trpc endpoint * Make the IntegrationAttributeSyncCard a dummy component * test: add tests for IntegrationAttributeSync feature Co-Authored-By: [email protected] <[email protected]> * Move creating a attribute sync record to the service * Add i18n strings * Safe select credential in find by id and team * Fix default credentialId value in form * Update repository return types * Add i18n string * Make credentialId optional for form schema * Fix label * Add cascade deletes * Add verification that syncs belong to org * Create mapper for repository output * Type fixes * Remove old test file * Pass `attributeOptions` from parent to children * Infer types from zod schema * Type fixes * Type fix * Clean up * Add i18n strings * Remove unused file * Address feedback * Add migration file * Address feedback * Add validation for new integration values * Remove unused router * Move away from z.infer to z.ZodType * Clean up comments * Type fix * Type fixes * Type fix * fix: add passthrough to syncFormDataSchema to preserve extra fields Co-Authored-By: [email protected] <[email protected]> * fix: remove incorrect test that expected extra fields to pass through syncFormDataSchema Co-Authored-By: [email protected] <[email protected]> * Add endpoint for SF to call * Create scratch org config * Create sf cli scripts * Create package logic * Update README * Remove unused file * Add indexes * Add aria label * Address feedback - consistent validation * Fix import paths for attribute types * Add `CredentialRepository.findByAppIdAndKeyValue` * Get credential by instance URL * Verify incoming sfdc orgId matches credential sfdc orgId * Rename method * Get user name integration syncs * refactor: change attributeSyncRules array to singular attributeSyncRule The database schema enforces a one-to-one relationship between IntegrationAttributeSync and AttributeSyncRule (via @unique constraint), and the UI only supports a single rule. This change makes the TypeScript type match the database schema and UI behavior. Changes: - Update IntegrationAttributeSync interface to use attributeSyncRule: AttributeSyncRule | null - Update mapper to return singular rule instead of wrapping in array - Update UI component to access sync.attributeSyncRule directly - Update IIntegrationAttributeSyncUpdateParams Omit type - Update tests to use attributeSyncRule: null instead of attributeSyncRules: [] Co-Authored-By: [email protected] <[email protected]> * Convert `membershipRepository.findAllByUserId` to a normal method * Add temp files to git ignore * Init and process team conditions * Biome formatting * Add `AttributeService` to get user attributes * Add DI container for AttributeService * AttributeSyncService evaluate attribute conditions * Create DI container for attributeSyncService * Return result for full condition * Evaluate if attribute sync should apply to user * Add method * Change PrismaAttributeOptionRepository to instance methods * Init AttributeSyncFieldMappingsService and process attribute syncs * Add AttributeSyncFieldMappingService DI container * Refactor orgId to organizationId * Add membership validation to sync field service * AttributeSYncFieldMappingService use repository methods * AttributeSyncFieldMappingService.processMappings add mapping logic * Add DI tokens * user-sync endpoint to implement attribute syncing * Validate team belongs to org for rule * test: add tests for AttributeSyncRuleService, AttributeSyncFieldMappingService, and AttributeService Co-Authored-By: [email protected] <[email protected]> * Remove duplicate migration file * Fix merge conflict * fix: resolve type errors in attribute sync feature (#26814) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * Rename variable * Fix log typo * Fix file name * Add error logging * Use credential teamId * fix: add missing mockTeamRepository and team validation to tests Co-Authored-By: [email protected] <[email protected]> * Fix naming * Fix file import * Type fix * Pass MembershipRepository as a dep in AttributeSyncFieldMappingService * Type fix * fix: add mockMembershipRepository to AttributeSyncFieldMappingService tests Co-Authored-By: [email protected] <[email protected]> * Update packages/app-store/salesforce/api/user-sync.ts Add error handling when getting orgId from stored salesforce id Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix: address Cubic code review comments - PrismaAttributeRepository: use nested select instead of include: true for options - CredentialRepository: use this.primaClient instead of global prisma, use select instead of include for relations - AttributeSyncFieldMappingService: optimize O(n*m) complexity with Map lookup for O(1) access Co-Authored-By: [email protected] <[email protected]> * Fix typo in CredentialRepository * Update README * Update sfdx-project * Add SFDC package tests * fix: improve Salesforce Apex test assertions to verify actual behavior - Enhanced CalComHttpMock to track HTTP callout invocations and capture requests - Updated UserUpdateHandlerTest to verify HTTP callouts are made with correct data - Updated CalComCalloutQueueableTest to verify HTTP callouts are made correctly - Replaced System.assert(true, ...) with meaningful assertions that verify: - Correct number of HTTP callouts - Request body contains expected fields - Request method is POST Addresses Cubic AI review feedback (confidence 9/10 issues only) Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
446 lines
9.7 KiB
TypeScript
446 lines
9.7 KiB
TypeScript
import { buildNonDelegationCredential } from "@calcom/lib/delegationCredential";
|
|
import logger from "@calcom/lib/logger";
|
|
import { prisma } from "@calcom/prisma";
|
|
import type { Prisma, PrismaClient } from "@calcom/prisma/client";
|
|
import { safeCredentialSelect } from "@calcom/prisma/selects/credential";
|
|
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["CredentialRepository"] });
|
|
|
|
type CredentialCreateInput = {
|
|
type: string;
|
|
key: object;
|
|
userId: number;
|
|
appId: string;
|
|
delegationCredentialId?: string | null;
|
|
};
|
|
|
|
type CredentialUpdateInput = {
|
|
type?: string;
|
|
key?: object;
|
|
userId?: number;
|
|
appId?: string;
|
|
delegationCredentialId?: string | null;
|
|
invalid?: boolean;
|
|
};
|
|
|
|
export class CredentialRepository {
|
|
constructor(private prismaClient: PrismaClient) {}
|
|
|
|
async findByCredentialId(id: number) {
|
|
return this.prismaClient.credential.findUnique({
|
|
where: { id },
|
|
select: safeCredentialSelect,
|
|
});
|
|
}
|
|
|
|
async findByIdWithDelegationCredential(id: number) {
|
|
return this.prismaClient.credential.findUnique({
|
|
where: { id },
|
|
select: {
|
|
...credentialForCalendarServiceSelect,
|
|
delegationCredential: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
static async create(data: CredentialCreateInput) {
|
|
const credential = await prisma.credential.create({ data: { ...data } });
|
|
return buildNonDelegationCredential(credential);
|
|
}
|
|
static async findByAppIdAndUserId({
|
|
appId,
|
|
userId,
|
|
}: {
|
|
appId: string;
|
|
userId: number;
|
|
}) {
|
|
const credential = await prisma.credential.findFirst({
|
|
where: {
|
|
appId,
|
|
userId,
|
|
},
|
|
});
|
|
return buildNonDelegationCredential(credential);
|
|
}
|
|
|
|
/**
|
|
* Doesn't retrieve key field as that has credentials
|
|
*/
|
|
static async findFirstByIdWithUser({ id }: { id: number }) {
|
|
const credential = await prisma.credential.findUnique({
|
|
where: { id },
|
|
select: safeCredentialSelect,
|
|
});
|
|
return buildNonDelegationCredential(credential);
|
|
}
|
|
|
|
/**
|
|
* Includes 'key' field which is sensitive data.
|
|
*/
|
|
static async findFirstByIdWithKeyAndUser({ id }: { id: number }) {
|
|
const credential = await prisma.credential.findUnique({
|
|
where: { id },
|
|
select: { ...safeCredentialSelect, key: true },
|
|
});
|
|
return buildNonDelegationCredential(credential);
|
|
}
|
|
|
|
static async findFirstByAppIdAndUserId({
|
|
appId,
|
|
userId,
|
|
}: {
|
|
appId: string;
|
|
userId: number;
|
|
}) {
|
|
return await prisma.credential.findFirst({
|
|
where: {
|
|
appId,
|
|
userId,
|
|
},
|
|
});
|
|
}
|
|
|
|
static async findFirstByUserIdAndType({
|
|
userId,
|
|
type,
|
|
}: {
|
|
userId: number;
|
|
type: string;
|
|
}) {
|
|
const credential = await prisma.credential.findFirst({
|
|
where: { userId, type },
|
|
});
|
|
return buildNonDelegationCredential(credential);
|
|
}
|
|
|
|
static async deleteById({ id }: { id: number }) {
|
|
await prisma.credential.delete({ where: { id } });
|
|
}
|
|
|
|
static async updateCredentialById({
|
|
id,
|
|
data,
|
|
}: {
|
|
id: number;
|
|
data: CredentialUpdateInput;
|
|
}) {
|
|
await prisma.credential.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
|
|
static async deleteAllByDelegationCredentialId({
|
|
delegationCredentialId,
|
|
}: {
|
|
delegationCredentialId: string;
|
|
}) {
|
|
return prisma.credential.deleteMany({ where: { delegationCredentialId } });
|
|
}
|
|
|
|
static async findCredentialForCalendarServiceById({ id }: { id: number }) {
|
|
const dbCredential = await prisma.credential.findUnique({
|
|
where: { id },
|
|
select: credentialForCalendarServiceSelect,
|
|
});
|
|
|
|
if (!dbCredential) {
|
|
return dbCredential;
|
|
}
|
|
|
|
return buildNonDelegationCredential(dbCredential);
|
|
}
|
|
|
|
static async findByIdIncludeDelegationCredential({ id }: { id: number }) {
|
|
const dbCredential = await prisma.credential.findUnique({
|
|
where: { id },
|
|
select: {
|
|
...credentialForCalendarServiceSelect,
|
|
delegationCredential: true,
|
|
},
|
|
});
|
|
|
|
return dbCredential;
|
|
}
|
|
|
|
static async findAllDelegationByUserIdsListAndDelegationCredentialIdAndType({
|
|
userIds,
|
|
delegationCredentialId,
|
|
type,
|
|
}: {
|
|
userIds: number[];
|
|
delegationCredentialId: string;
|
|
type: string;
|
|
}) {
|
|
return prisma.credential.findMany({
|
|
where: {
|
|
userId: {
|
|
in: userIds,
|
|
},
|
|
delegationCredentialId,
|
|
type,
|
|
},
|
|
select: {
|
|
userId: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
static async findAllDelegationByTypeIncludeUserAndTake({
|
|
type,
|
|
take,
|
|
}: {
|
|
type: string;
|
|
take: number;
|
|
}) {
|
|
const delegationUserCredentials = await prisma.credential.findMany({
|
|
where: {
|
|
delegationCredentialId: { not: null },
|
|
type,
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
id: true,
|
|
},
|
|
},
|
|
},
|
|
take,
|
|
});
|
|
return delegationUserCredentials.map(
|
|
({ delegationCredentialId, ...rest }) => {
|
|
return {
|
|
...rest,
|
|
// We queried only those where delegationCredentialId is not null
|
|
|
|
delegationCredentialId: delegationCredentialId!,
|
|
};
|
|
}
|
|
);
|
|
}
|
|
|
|
static async findUniqueByUserIdAndDelegationCredentialId({
|
|
userId,
|
|
delegationCredentialId,
|
|
}: {
|
|
userId: number;
|
|
delegationCredentialId: string;
|
|
}) {
|
|
const delegationUserCredentials = await prisma.credential.findMany({
|
|
where: {
|
|
userId,
|
|
delegationCredentialId,
|
|
},
|
|
});
|
|
|
|
if (delegationUserCredentials.length > 1) {
|
|
// Instead of crashing use the first one and log for observability
|
|
// TODO: Plan to add a unique constraint on userId and delegationCredentialId
|
|
log.error(
|
|
`DelegationCredential: Multiple delegation user credentials found - this should not happen`,
|
|
{
|
|
userId,
|
|
delegationCredentialId,
|
|
}
|
|
);
|
|
}
|
|
|
|
return delegationUserCredentials[0];
|
|
}
|
|
|
|
static async updateWhereUserIdAndDelegationCredentialId({
|
|
userId,
|
|
delegationCredentialId,
|
|
data,
|
|
}: {
|
|
userId: number;
|
|
delegationCredentialId: string;
|
|
data: {
|
|
key: Prisma.InputJsonValue;
|
|
};
|
|
}) {
|
|
return prisma.credential.updateMany({
|
|
where: {
|
|
userId,
|
|
delegationCredentialId,
|
|
},
|
|
data,
|
|
});
|
|
}
|
|
|
|
static async createDelegationCredential({
|
|
userId,
|
|
delegationCredentialId,
|
|
type,
|
|
key,
|
|
appId,
|
|
}: {
|
|
userId: number;
|
|
delegationCredentialId: string;
|
|
type: string;
|
|
key: Prisma.InputJsonValue;
|
|
appId: string;
|
|
}) {
|
|
return prisma.credential.create({
|
|
data: { userId, delegationCredentialId, type, key, appId },
|
|
});
|
|
}
|
|
|
|
static async updateWhereId({
|
|
id,
|
|
data,
|
|
}: {
|
|
id: number;
|
|
data: { key: Prisma.InputJsonValue };
|
|
}) {
|
|
return prisma.credential.update({ where: { id }, data });
|
|
}
|
|
|
|
static async findPaymentCredentialByAppIdAndTeamId({
|
|
appId,
|
|
teamId,
|
|
}: {
|
|
appId: string | null;
|
|
teamId: number;
|
|
}) {
|
|
return await prisma.credential.findFirst({
|
|
where: {
|
|
teamId,
|
|
appId,
|
|
},
|
|
include: {
|
|
app: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
static async findPaymentCredentialByAppIdAndUserId({
|
|
appId,
|
|
userId,
|
|
}: {
|
|
appId: string | null;
|
|
userId: number;
|
|
}) {
|
|
return await prisma.credential.findFirst({
|
|
where: {
|
|
userId,
|
|
appId,
|
|
},
|
|
include: {
|
|
app: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
static async findPaymentCredentialByAppIdAndUserIdOrTeamId({
|
|
appId,
|
|
userId,
|
|
teamId,
|
|
}: {
|
|
appId: string | null;
|
|
userId: number;
|
|
teamId?: number | null;
|
|
}) {
|
|
const idToSearchObject = teamId ? { teamId } : { userId };
|
|
return await prisma.credential.findFirst({
|
|
where: {
|
|
...idToSearchObject,
|
|
appId,
|
|
},
|
|
include: {
|
|
app: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
findByTeamIdAndSlugs({ teamId, slugs }: { teamId: number; slugs: string[] }) {
|
|
return this.prismaClient.credential.findMany({
|
|
where: {
|
|
teamId,
|
|
appId: {
|
|
in: slugs,
|
|
},
|
|
},
|
|
select: { ...safeCredentialSelect, team: { select: { name: true } } },
|
|
});
|
|
}
|
|
|
|
findByIdAndTeamId({ id, teamId }: { id: number; teamId: number }) {
|
|
return this.prismaClient.credential.findFirst({
|
|
where: {
|
|
id,
|
|
teamId,
|
|
},
|
|
select: {
|
|
...safeCredentialSelect,
|
|
app: {
|
|
select: {
|
|
slug: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async findByAppIdAndKeyValue({
|
|
appId,
|
|
keyPath,
|
|
value,
|
|
keyFields,
|
|
}: {
|
|
appId: string;
|
|
keyPath: string[];
|
|
value: Prisma.InputJsonValue;
|
|
keyFields?: string[];
|
|
}) {
|
|
const credential = await this.prismaClient.credential.findFirst({
|
|
where: {
|
|
appId,
|
|
key: {
|
|
path: keyPath,
|
|
equals: value,
|
|
},
|
|
},
|
|
select: {
|
|
...safeCredentialSelect,
|
|
integrationAttributeSyncs: {
|
|
select: {
|
|
id: true,
|
|
attributeSyncRule: {
|
|
select: {
|
|
id: true,
|
|
rule: true,
|
|
},
|
|
},
|
|
syncFieldMappings: {
|
|
select: {
|
|
id: true,
|
|
integrationFieldName: true,
|
|
attributeId: true,
|
|
enabled: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
key: keyFields ? true : false,
|
|
},
|
|
});
|
|
|
|
if (!credential || !keyFields) {
|
|
return credential;
|
|
}
|
|
|
|
const key = credential.key as Record<string, unknown>;
|
|
const filteredKey = keyFields.reduce((acc, field) => {
|
|
if (field in key) {
|
|
acc[field] = key[field];
|
|
}
|
|
return acc;
|
|
}, {} as Record<string, unknown>);
|
|
|
|
return { ...credential, key: filteredKey };
|
|
}
|
|
}
|