@memberjunction/actions-bizapps-crm
Version:
CRM system integration actions for MemberJunction
203 lines • 8.6 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeleteContactAction = void 0;
const global_1 = require("@memberjunction/global");
const hubspot_base_action_1 = require("../hubspot-base.action");
const actions_1 = require("@memberjunction/actions");
/**
* Action to delete/archive a contact in HubSpot
*/
let DeleteContactAction = class DeleteContactAction extends hubspot_base_action_1.HubSpotBaseAction {
/**
* Delete/archive a contact
*/
async InternalRunAction(params) {
const { Params, ContextUser } = params;
this.params = Params; // Set params for base class to use
try {
// Extract and validate parameters
const contactId = this.getParamValue(Params, 'ContactId');
const email = this.getParamValue(Params, 'Email');
const permanentDelete = this.getParamValue(Params, 'PermanentDelete') || false;
const archiveOnly = this.getParamValue(Params, 'ArchiveOnly') || true;
if (!contactId && !email) {
return {
Success: false,
ResultCode: 'VALIDATION_ERROR',
Message: 'Either ContactId or Email is required',
Params
};
}
let contactToDelete;
let actualContactId;
// If email provided, search for contact first
if (email && !contactId) {
const searchResults = await this.searchHubSpotObjects('contacts', [{
propertyName: 'email',
operator: 'EQ',
value: email
}], ['email', 'firstname', 'lastname', 'company'], ContextUser);
if (searchResults.length === 0) {
return {
Success: false,
ResultCode: 'CONTACT_NOT_FOUND',
Message: `No contact found with email ${email}`,
Params
};
}
if (searchResults.length > 1) {
return {
Success: false,
ResultCode: 'MULTIPLE_CONTACTS_FOUND',
Message: `Multiple contacts found with email ${email}. Please use ContactId instead.`,
Params
};
}
contactToDelete = searchResults[0];
actualContactId = contactToDelete.id;
}
else {
actualContactId = contactId;
// Get contact details before deletion
try {
contactToDelete = await this.makeHubSpotRequest(`objects/contacts/${actualContactId}`, 'GET', undefined, ContextUser);
}
catch (getError) {
if (getError.message.includes('404')) {
return {
Success: false,
ResultCode: 'CONTACT_NOT_FOUND',
Message: `Contact with ID ${actualContactId} not found`,
Params
};
}
throw getError;
}
}
// Store contact details before deletion
const contactDetails = this.mapHubSpotProperties(contactToDelete);
const deletionTime = new Date().toISOString();
// Perform deletion based on parameters
if (permanentDelete && !archiveOnly) {
// Permanent deletion (GDPR compliant)
await this.makeHubSpotRequest(`objects/contacts/${actualContactId}/gdpr-delete`, 'POST', undefined, ContextUser);
}
else {
// Archive contact (soft delete)
await this.makeHubSpotRequest(`objects/contacts/${actualContactId}`, 'DELETE', undefined, ContextUser);
}
// Create deletion summary
const summary = {
contactId: actualContactId,
email: contactDetails.email,
fullName: `${contactDetails.firstname || ''} ${contactDetails.lastname || ''}`.trim(),
company: contactDetails.company,
deletionType: permanentDelete && !archiveOnly ? 'permanent' : 'archived',
deletedAt: deletionTime,
wasActive: !contactDetails.archived,
lifecycleStageAtDeletion: contactDetails.lifecyclestage,
createdAt: contactDetails.createdAt,
lastModifiedAt: contactDetails.updatedAt
};
// Update output parameters
const outputParams = [...Params];
const deletedContactParam = outputParams.find(p => p.Name === 'DeletedContact');
if (deletedContactParam)
deletedContactParam.Value = contactDetails;
const summaryParam = outputParams.find(p => p.Name === 'Summary');
if (summaryParam)
summaryParam.Value = summary;
const message = permanentDelete && !archiveOnly
? `Permanently deleted contact ${contactDetails.email || actualContactId}`
: `Archived contact ${contactDetails.email || actualContactId}`;
return {
Success: true,
ResultCode: 'SUCCESS',
Message: message,
Params: outputParams
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
// Check for specific error types
if (errorMessage.includes('404') || errorMessage.includes('not found')) {
return {
Success: false,
ResultCode: 'CONTACT_NOT_FOUND',
Message: 'Contact not found',
Params
};
}
if (errorMessage.includes('403') || errorMessage.includes('forbidden')) {
return {
Success: false,
ResultCode: 'PERMISSION_DENIED',
Message: 'Permission denied to delete this contact',
Params
};
}
return {
Success: false,
ResultCode: 'ERROR',
Message: `Error deleting contact: ${errorMessage}`,
Params
};
}
}
/**
* Define the parameters this action expects
*/
get Params() {
const baseParams = this.getCommonCRMParams();
const specificParams = [
{
Name: 'ContactId',
Type: 'Input',
Value: null
},
{
Name: 'Email',
Type: 'Input',
Value: null
},
{
Name: 'PermanentDelete',
Type: 'Input',
Value: false
},
{
Name: 'ArchiveOnly',
Type: 'Input',
Value: true
},
{
Name: 'DeletedContact',
Type: 'Output',
Value: null
},
{
Name: 'Summary',
Type: 'Output',
Value: null
}
];
return [...baseParams, ...specificParams];
}
/**
* Metadata about this action
*/
get Description() {
return 'Deletes or archives a contact in HubSpot by ID or email with GDPR compliance options';
}
};
exports.DeleteContactAction = DeleteContactAction;
exports.DeleteContactAction = DeleteContactAction = __decorate([
(0, global_1.RegisterClass)(actions_1.BaseAction, 'DeleteContactAction')
], DeleteContactAction);
//# sourceMappingURL=delete-contact.action.js.map