The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { isNonEmptyString } from '@sniptt/guards';
import { defineLogicFunction } from 'twenty-sdk/define';
import {
createMessageChannel,
getConnection,
kv,
listMessageChannels,
} from 'twenty-sdk/logic-function';
import { isDefined } from 'twenty-sdk/utils';
import { PERSON_LINKEDIN_LINK_FIELD_UNIVERSAL_IDENTIFIER } from 'src/constants/person-linkedin-link-field-universal-identifier.constant';
import {
LINKEDIN_MESSAGING_PROVIDER_UNIVERSAL_IDENTIFIER,
LINKEDIN_ON_CONNECT_UNIVERSAL_IDENTIFIER,
} from 'src/constants/universal-identifiers';
import { type LinkedinConnectionHookPayload } from 'src/logic-functions/types/linkedin-connection-hook-payload.type';
import { bindLinkedinMessagingProvider } from 'src/logic-functions/utils/bind-linkedin-messaging-provider.util';
import { buildLinkedinProfileHandle } from 'src/logic-functions/utils/build-linkedin-profile-handle.util';
import { getUnipileAccountClaimKey } from 'src/unipile/utils/get-unipile-account-claim-key.util';
import { getUnipileUser } from 'src/unipile/utils/get-unipile-user.util';
export const linkedinOnConnectHandler = async ({
connectedAccountId,
}: LinkedinConnectionHookPayload): Promise<{ messageChannelId: string }> => {
const connection = await getConnection(connectedAccountId);
const unipileAccountId = connection.externalAccountId;
if (!isNonEmptyString(unipileAccountId)) {
throw new Error(
`LinkedIn connection ${connectedAccountId} has no Unipile account`,
);
}
const [existingChannel] = await listMessageChannels({ connectedAccountId });
if (isDefined(existingChannel)) {
await bindLinkedinMessagingProvider(existingChannel);
}
const channel =
existingChannel ??
(await (async () => {
const profile = await getUnipileUser({
accountId: unipileAccountId,
userId: 'me',
});
return createMessageChannel({
connectedAccountId,
handle: buildLinkedinProfileHandle(profile),
displayName: profile.display_name,
visibility: 'METADATA',
participantHandleFieldUniversalIdentifier:
PERSON_LINKEDIN_LINK_FIELD_UNIVERSAL_IDENTIFIER,
messagingProvider: {
universalIdentifier: LINKEDIN_MESSAGING_PROVIDER_UNIVERSAL_IDENTIFIER,
},
});
})());
await kv.set(getUnipileAccountClaimKey(unipileAccountId), undefined, {
scope: 'SERVER',
});
return { messageChannelId: channel.id };
};
export default defineLogicFunction({
universalIdentifier: LINKEDIN_ON_CONNECT_UNIVERSAL_IDENTIFIER,
name: 'linkedin-on-connect',
description:
'Creates the message channel of a new LinkedIn connection, lets members reply through it and routes its Unipile webhooks to this workspace.',
timeoutSeconds: 30,
handler: linkedinOnConnectHandler,
});