Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: discord integration #381

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fern/definition/internal/account.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ service:
tpId: types.TPID
isRevertApp: boolean
appId: string
botToken: optional<string>
response: types.App
errors:
- errors.UnAuthorizedError
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const config = {
LOOPS_API_KEY: process.env.LOOPS_API_KEY,
CLOSECRM_CLIENT_ID: process.env.CLOSECRM_CLIENT_ID!,
CLOSECRM_CLIENT_SECRET: process.env.CLOSECRM_CLIENT_SECRET!,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID!,
DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET!,
DISCORD_BOT_TOKEN: process.env.DISCORD_BOT_TOKEN!,
};

export default config;
10 changes: 9 additions & 1 deletion packages/backend/constants/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TP_ID } from '@prisma/client';

export type CRM_TP_ID = 'zohocrm' | 'sfdc' | 'pipedrive' | 'hubspot' | 'closecrm';
// export type CHAT_TP_ID = 'slack';
export type CHAT_TP_ID = 'slack' | 'discord';

export const DEFAULT_SCOPE = {
[TP_ID.hubspot]: [
Expand Down Expand Up @@ -39,6 +39,7 @@ export const DEFAULT_SCOPE = {
[TP_ID.pipedrive]: [],
[TP_ID.slack]: ['users:read', 'users.profile:read'],
[TP_ID.closecrm]: [],
[TP_ID.discord]: ['identify', 'bot'],
};

export const mapIntegrationIdToIntegrationName = {
Expand All @@ -48,6 +49,7 @@ export const mapIntegrationIdToIntegrationName = {
[TP_ID.zohocrm]: 'Zoho',
[TP_ID.slack]: 'Slack',
[TP_ID.closecrm]: 'Close',
[TP_ID.discord]: 'Discord',
};

export const rootSchemaMappingId = 'revertRootSchemaMapping';
Expand All @@ -63,6 +65,12 @@ export enum StandardObjects {
user = 'user',
}

export enum ChatStandardObjects {
channel = 'channel',
chatUser = 'chatUser',
message = 'message',
}

export const objectNameMapping: Record<string, Record<CRM_TP_ID, string | undefined>> = {
[StandardObjects.company]: {
[TP_ID.hubspot]: 'companies',
Expand Down
34 changes: 33 additions & 1 deletion packages/backend/helpers/crm/transform/disunify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TP_ID, accountFieldMappingConfig } from '@prisma/client';
import { CRM_TP_ID, StandardObjects } from '../../../constants/common';
import { CHAT_TP_ID, CRM_TP_ID, ChatStandardObjects, StandardObjects } from '../../../constants/common';
import { transformModelToFieldMapping } from '.';
import { handleHubspotDisunify, handlePipedriveDisunify, handleSfdcDisunify, handleZohoDisunify } from '..';
import { postprocessDisUnifyObject } from './preprocess';
Expand Down Expand Up @@ -46,3 +46,35 @@ export async function disunifyObject<T extends Record<string, any>>({
}
}
}

export async function disunifyChatObject<T extends Record<string, any>>({
obj,
tpId,
objType,
tenantSchemaMappingId,
accountFieldMappingConfig,
}: {
obj: T;
tpId: CHAT_TP_ID;
objType: ChatStandardObjects;
tenantSchemaMappingId?: string;
accountFieldMappingConfig?: accountFieldMappingConfig;
}) {
const flattenedObj = flattenObj(obj, ['additional']);
const transformedObj = await transformModelToFieldMapping({
unifiedObj: flattenedObj,
tpId,
objType,
tenantSchemaMappingId,
accountFieldMappingConfig,
});

switch (tpId) {
case TP_ID.slack: {
return transformedObj;
}
case TP_ID.discord: {
return transformedObj;
}
}
}
4 changes: 2 additions & 2 deletions packages/backend/helpers/crm/transform/preprocess.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TP_ID } from '@prisma/client';
import { CRM_TP_ID, StandardObjects } from '../../../constants/common';
import { CRM_TP_ID, ChatStandardObjects, StandardObjects } from '../../../constants/common';
import { PipedriveDealStatus } from '../../../constants/pipedrive';
import { convertToHHMMInUTC, getDuration, getFormattedDate } from '../../../helpers/timeZoneHelper';

Expand All @@ -10,7 +10,7 @@ export const preprocessUnifyObject = <T extends Record<string, any>>({
}: {
obj: T;
tpId: CRM_TP_ID;
objType: StandardObjects;
objType: StandardObjects | ChatStandardObjects;
}) => {
const preprocessMap: any = {
[TP_ID.pipedrive]: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get, merge } from 'lodash';
import { Prisma, PrismaClient, TP_ID, accountFieldMappingConfig } from '@prisma/client';
import { StandardObjects, rootSchemaMappingId } from '../../../constants/common';
import { ChatStandardObjects, StandardObjects, rootSchemaMappingId } from '../../../constants/common';
import { logDebug } from '../../logger';

const prisma = new PrismaClient();
Expand All @@ -14,7 +14,7 @@ export const transformFieldMappingToModel = async ({
}: {
obj: any;
tpId: TP_ID;
objType: StandardObjects;
objType: StandardObjects | ChatStandardObjects;
tenantSchemaMappingId?: string;
accountFieldMappingConfig?: accountFieldMappingConfig;
}) => {
Expand Down Expand Up @@ -67,7 +67,7 @@ export const transformModelToFieldMapping = async ({
}: {
unifiedObj: any;
tpId: TP_ID;
objType: StandardObjects;
objType: StandardObjects | ChatStandardObjects;
tenantSchemaMappingId?: string;
accountFieldMappingConfig?: accountFieldMappingConfig;
}) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/helpers/crm/transform/unify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { accountFieldMappingConfig } from '@prisma/client';
import { CRM_TP_ID, StandardObjects } from '../../../constants/common';
import { CRM_TP_ID, ChatStandardObjects, StandardObjects } from '../../../constants/common';
import { transformFieldMappingToModel } from '.';
import { preprocessUnifyObject } from './preprocess';

Expand All @@ -12,7 +12,7 @@ export async function unifyObject<T extends Record<string, any>, K>({
}: {
obj: T;
tpId: CRM_TP_ID;
objType: StandardObjects;
objType: StandardObjects | ChatStandardObjects;
tenantSchemaMappingId?: string;
accountFieldMappingConfig?: accountFieldMappingConfig;
}): Promise<K> {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/helpers/tenantIdMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const revertTenantMiddleware = () => async (req: Request, res: Response, next: (
t_id: true,
tp_account_url: true,
tp_customer_id: true,
app_bot_token: true,
app: {
include: {
env: {
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/models/unified/channel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export interface UnifiedChannel {
id: string;
name: string;
createdTimeStamp: string;
createdTimeStamp: string | null;
additional: any;
}

export function unifyChannel(channel: any) {
const unifiedChannel: UnifiedChannel = {
id: channel.id,
name: channel.name_normalized,
createdTimeStamp: new Date(channel.created * 1000).toISOString(),
name: channel.name_normalized || channel.name,
createdTimeStamp: channel.created ? new Date(channel.created * 1000).toISOString() : null,
additional: {},
};

Expand Down
6 changes: 3 additions & 3 deletions packages/backend/models/unified/chatUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export interface UnifiedChatUser {

export function unifyChatUser(user: any): UnifiedChatUser {
const unifiedUser: UnifiedChatUser = {
id: user.id,
name: user.real_name,
createdTimeStamp: new Date(user.updated * 1000).toISOString(),
id: user.id || user.user.id,
name: user.profile.real_name || user.user.global_name,
jatinsandilya marked this conversation as resolved.
Show resolved Hide resolved
createdTimeStamp: user.joined_at || new Date(user.updated * 1000).toISOString(),
additional: {},
};

Expand Down
18 changes: 17 additions & 1 deletion packages/backend/models/unified/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TP_ID } from '@prisma/client';

export interface UnifiedMessage {
text: string;
channelId: string;
channelId: string | undefined;
jatinsandilya marked this conversation as resolved.
Show resolved Hide resolved
additional?: any;
}

Expand All @@ -26,6 +26,8 @@ export function unifyMessage(message: any) {
export function disunifyMessage(message: UnifiedMessage, integrationId: string): any {
if (integrationId === TP_ID.slack) {
return toSlackMessage(message);
} else if (integrationId === TP_ID.discord) {
return toDiscordMessage(message);
}
}

Expand All @@ -44,3 +46,17 @@ function toSlackMessage(message: UnifiedMessage): any {

return slackMessage;
}
function toDiscordMessage(message: UnifiedMessage): any {
const discordMessage: any = {
content: message.text,
};

// Map custom fields, if any
if (message.additional) {
Object.keys(message.additional).forEach((key) => {
discordMessage[key] = message.additional?.[key];
});
}

return discordMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "TP_ID" ADD VALUE 'discord';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "apps" ADD COLUMN "app_bot_token" TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "connections" ADD COLUMN "app_bot_token" TEXT;
3 changes: 3 additions & 0 deletions packages/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum TP_ID {
pipedrive
closecrm
slack
discord
}

enum ENV {
Expand Down Expand Up @@ -55,6 +56,7 @@ model apps {
app_client_id String?
app_client_secret String?
owner_account_public_token String?
app_bot_token String?
environmentId String
env environments @relation(fields: [environmentId], references: [id])
connections connections[]
Expand Down Expand Up @@ -84,6 +86,7 @@ model connections {
owner_account_public_token String
app_client_id String?
app_client_secret String?
app_bot_token String?
app apps? @relation(fields: [appId], references: [id])
appId String?
schema_mapping_id String?
Expand Down
Loading