aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/copper/index.ts
blob: 6c0c5abd59f2f77db67f6dfda5816a48d626956b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import * as R from 'ramda';

import { CopperActivity, CopperActivityType, CopperCustomField, CopperLead, CopperOpportunity } from '../../entities';

const ONE_SECOND = 1000;
export type CopperSearchResponse = CopperLeadResponse | CopperActivityResponse | CopperOpportunityResponse;
export interface CopperLeadResponse {
    id: number;
    name?: string;
    first_name?: string;
    last_name?: string;
    middle_name?: string;
    assignee_id?: number;
    company_name?: string;
    customer_source_id?: number;
    monetary_value?: number;
    status: string;
    status_id: number;
    title?: string;
    date_created: number; // in seconds
    date_modified: number; // in seconds
}

export interface CopperActivityResponse {
    id: number;
    parent: CopperActivityParentResponse;
    type: CopperActivityTypeResponse;
    user_id: number;
    activity_date: number;
    old_value: CopperActivityValueResponse;
    new_value: CopperActivityValueResponse;
    date_created: number; // in seconds
    date_modified: number; // in seconds
}

export interface CopperActivityValueResponse {
    id: number;
    name: string;
}
export interface CopperActivityParentResponse {
    id: number;
    type: string;
}

// custom activity types
export enum CopperActivityTypeCategory {
    user = 'user',
    system = 'system',
}
export interface CopperActivityTypeResponse {
    id: number;
    category: CopperActivityTypeCategory;
    name: string;
    is_disabled?: boolean;
    count_as_interaction?: boolean;
}

export interface CopperOpportunityResponse {
    id: number;
    name: string;
    assignee_id?: number;
    close_date?: string;
    company_id?: number;
    company_name?: string;
    customer_source_id?: number;
    loss_reason_id?: number;
    pipeline_id: number;
    pipeline_stage_id: number;
    primary_contact_id?: number;
    priority?: string;
    status: string;
    tags: string[];
    interaction_count: number;
    monetary_value?: number;
    win_probability?: number;
    date_created: number; // in seconds
    date_modified: number; // in seconds
    custom_fields: CopperNestedCustomFieldResponse[];
}
interface CopperNestedCustomFieldResponse {
    custom_field_definition_id: number;
    value: number | number[] | null;
}
// custom fields
export enum CopperCustomFieldType {
    String = 'String',
    Text = 'Text',
    Dropdown = 'Dropdown',
    MultiSelect = 'MultiSelect', // not in API documentation but shows up in results
    Date = 'Date',
    Checkbox = 'Checkbox',
    Float = 'Float',
    URL = 'URL',
    Percentage = 'Percentage',
    Currency = 'Currency',
    Connect = 'Connect',
}
export interface CopperCustomFieldOptionResponse {
    id: number;
    name: string;
}
export interface CopperCustomFieldResponse {
    id: number;
    name: string;
    data_type: CopperCustomFieldType;
    options?: CopperCustomFieldOptionResponse[];
}
/**
 * Parse response from Copper API /search/leads/
 *
 * @param leads - The array of leads returned from the API
 * @returns Returns an array of Copper Lead entities
 */
export function parseLeads(leads: CopperLeadResponse[]): CopperLead[] {
    return leads.map(lead => {
        const entity = new CopperLead();
        entity.id = lead.id;
        entity.name = lead.name || undefined;
        entity.firstName = lead.first_name || undefined;
        entity.lastName = lead.last_name || undefined;
        entity.middleName = lead.middle_name || undefined;
        entity.assigneeId = lead.assignee_id || undefined;
        entity.companyName = lead.company_name || undefined;
        entity.customerSourceId = lead.customer_source_id || undefined;
        entity.monetaryValue = lead.monetary_value || undefined;
        entity.status = lead.status;
        entity.statusId = lead.status_id;
        entity.title = lead.title || undefined;
        entity.dateCreated = lead.date_created * ONE_SECOND;
        entity.dateModified = lead.date_modified * ONE_SECOND;
        return entity;
    });
}

/**
 * Parse response from Copper API /search/activities/
 *
 * @param activities - The array of activities returned from the API
 * @returns Returns an array of Copper Activity entities
 */
export function parseActivities(activities: CopperActivityResponse[]): CopperActivity[] {
    return activities.map(activity => {
        const entity = new CopperActivity();
        entity.id = activity.id;

        entity.parentId = activity.parent.id;
        entity.parentType = activity.parent.type;

        entity.typeId = activity.type.id;
        entity.typeCategory = activity.type.category.toString();
        entity.typeName = activity.type.name;

        entity.userId = activity.user_id;
        entity.dateCreated = activity.date_created * ONE_SECOND;
        entity.dateModified = activity.date_modified * ONE_SECOND;

        // nested nullable fields
        entity.oldValueId = R.path(['old_value', 'id'], activity);
        entity.oldValueName = R.path(['old_value', 'name'], activity);
        entity.newValueId = R.path(['new_value', 'id'], activity);
        entity.newValueName = R.path(['new_value', 'name'], activity);

        return entity;
    });
}

/**
 * Parse response from Copper API /search/opportunities/
 *
 * @param opportunities - The array of opportunities returned from the API
 * @returns Returns an array of Copper Opportunity entities
 */
export function parseOpportunities(opportunities: CopperOpportunityResponse[]): CopperOpportunity[] {
    return opportunities.map(opp => {
        const customFields: { [key: number]: number } = opp.custom_fields
            .filter(f => f.value !== null)
            .map(f => ({
                ...f,
                value: ([] as number[]).concat(f.value || []), // normalise all values to number[]
            }))
            .map(f => f.value.map(val => [f.custom_field_definition_id, val] as [number, number])) // pair each value with the custom_field_definition_id
            .reduce((acc, pair) => acc.concat(pair)) // flatten
            .reduce<{ [key: number]: number }>((obj, [key, value]) => {
                // transform into object literal
                obj[key] = value;
                return obj;
            }, {});

        const entity = new CopperOpportunity();
        entity.id = opp.id;
        entity.name = opp.name;
        entity.assigneeId = opp.assignee_id || undefined;
        entity.closeDate = opp.close_date || undefined;
        entity.companyId = opp.company_id || undefined;
        entity.companyName = opp.company_name || undefined;
        entity.customerSourceId = opp.customer_source_id || undefined;
        entity.lossReasonId = opp.loss_reason_id || undefined;
        entity.pipelineId = opp.pipeline_id;
        entity.pipelineStageId = opp.pipeline_stage_id;
        entity.primaryContactId = opp.primary_contact_id || undefined;
        entity.priority = opp.priority || undefined;
        entity.status = opp.status;
        entity.interactionCount = opp.interaction_count;
        entity.monetaryValue = opp.monetary_value || undefined;
        entity.winProbability = opp.win_probability === null ? undefined : opp.win_probability;
        entity.dateCreated = opp.date_created * ONE_SECOND;
        entity.dateModified = opp.date_modified * ONE_SECOND;
        entity.customFields = customFields;
        return entity;
    });
}

/**
 * Parse response from Copper API /activity_types/
 *
 * @param activityTypeResponse - Activity Types response from the API, keyed by "user" or "system"
 * @returns Returns an array of Copper Activity Type entities
 */
export function parseActivityTypes(
    activityTypeResponse: Map<CopperActivityTypeCategory, CopperActivityTypeResponse[]>,
): CopperActivityType[] {
    const values: CopperActivityTypeResponse[] = R.flatten(Object.values(activityTypeResponse));
    return values.map(activityType => ({
        id: activityType.id,
        name: activityType.name,
        category: activityType.category.toString(),
        isDisabled: activityType.is_disabled,
        countAsInteraction: activityType.count_as_interaction,
    }));
}

/**
 * Parse response from Copper API /custom_field_definitions/
 *
 * @param customFieldResponse - array of custom field definitions returned from the API, consisting of top-level fields and nested fields
 * @returns Returns an array of Copper Custom Field entities
 */
export function parseCustomFields(customFieldResponse: CopperCustomFieldResponse[]): CopperCustomField[] {
    function parseTopLevelField(field: CopperCustomFieldResponse): CopperCustomField[] {
        const topLevelField: CopperCustomField = {
            id: field.id,
            name: field.name,
            dataType: field.data_type.toString(),
        };

        if (field.options !== undefined) {
            const nestedFields: CopperCustomField[] = field.options.map(option => ({
                id: option.id,
                name: option.name,
                dataType: field.name,
                fieldType: 'option',
            }));
            return nestedFields.concat(topLevelField);
        } else {
            return [topLevelField];
        }
    }
    return R.chain(parseTopLevelField, customFieldResponse);
}