1import { Actor } from "apify";
2
3await Actor.init();
4const { datasetIds, mergeToOneCell = false } = await Actor.getInput();
5
6
7const transformFunction = (items, { customInputData }) => {
8 const CONTACT_FIELDS = ['emails', 'phones', 'phonesUncertain', 'linkedIns', 'twitters', 'instagrams', 'facebooks',
9 'youtubes', 'tiktoks', 'pinterests', 'discords'];
10
11 const contactsPerDomain = {};
12
13 const { mergeToOneCell } = customInputData;
14
15 for (const item of items) {
16 const { domain } = item;
17
18 for (const contactField of CONTACT_FIELDS) {
19 contactsPerDomain[domain] ??= {};
20 contactsPerDomain[domain][contactField] ??= new Set();
21 for (const contact of item[contactField] || []) {
22 contactsPerDomain[domain][contactField].add(contact);
23 }
24 }
25 }
26
27
28 const results = [];
29 for (const [domain, contacts] of Object.entries(contactsPerDomain)) {
30 const contactsFormatted = {};
31 for (const [contactField, contactSet] of Object.entries(contacts)) {
32 contactsFormatted[contactField] = [...contactSet];
33 if (mergeToOneCell) {
34 contactsFormatted[contactField] = contactsFormatted[contactField].join('; ');
35 }
36 }
37 results.push({
38 domain,
39 ...contactsFormatted,
40 });
41 }
42 return results;
43}
44
45await Actor.metamorph(
46 'lukaskrivka/dedup-datasets',
47 {
48 datasetIds,
49 preDedupTransformFunction: transformFunction,
50 customInputData: { mergeToOneCell },
51 }
52)
53
54
55await Actor.pushData(headings);
56
57
58await Actor.exit();