abmp-npm 10.0.55 → 10.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ const { incrementUrlCounter, extractBaseUrl } = require('../daily-pull/utils');
2
+ const {
3
+ normalizeUrlForComparison,
4
+ sortByUrlCounterDescending,
5
+ extractUrlCounter,
6
+ } = require('../utils');
7
+
8
+ // ─── Helpers ─────────────────────────────────────────────────────────
9
+
10
+ /**
11
+ * Simulates getMemberBySlug's normalized-comparison branch using
12
+ * the ACTUAL production sort comparator imported from utils.js.
13
+ */
14
+ function simulateGetHighestMember(allMembers, slug) {
15
+ const matching = allMembers.filter(
16
+ m => m.url && normalizeUrlForComparison(m.url) === slug.toLowerCase()
17
+ );
18
+ matching.sort(sortByUrlCounterDescending);
19
+ return matching[0] || null;
20
+ }
21
+
22
+ /**
23
+ * Simulates ensureUniqueUrl's counter-increment logic given the
24
+ * "highest" member returned by getMemberBySlug.
25
+ */
26
+ function simulateEnsureUniqueUrl(baseSlug, highestMember) {
27
+ if (!highestMember || !highestMember.url) return baseSlug;
28
+ return incrementUrlCounter(highestMember?.url, baseSlug);
29
+ }
30
+
31
+ // ─── Test data ───────────────────────────────────────────────────────
32
+
33
+ function buildMembersInDb() {
34
+ return [
35
+ { memberId: 1, url: 'firstNameLastName' },
36
+ { memberId: 2, url: 'firstNameLastName-1' },
37
+ { memberId: 3, url: 'firstNameLastName-2' },
38
+ { memberId: 4, url: 'firstNameLastName-3' },
39
+ { memberId: 5, url: 'firstNameLastName-4' },
40
+ { memberId: 6, url: 'firstNameLastName-5' },
41
+ { memberId: 7, url: 'firstNameLastName-6' },
42
+ { memberId: 8, url: 'firstNameLastName-7' },
43
+ { memberId: 9, url: 'firstNameLastName-8' },
44
+ { memberId: 10, url: 'firstNameLastName-9' },
45
+ { memberId: 11, url: 'firstNameLastName-10' },
46
+ { memberId: 12, url: 'firstNameLastName-11' },
47
+ { memberId: 13, url: 'firstNameLastName-12' },
48
+ ];
49
+ }
50
+
51
+ function buildLargeCounterMembers() {
52
+ const members = [];
53
+ for (let i = 0; i <= 105; i++) {
54
+ members.push({ memberId: i, url: i === 0 ? 'testUser' : `testUser-${i}` });
55
+ }
56
+ return members;
57
+ }
58
+
59
+ // ─── Tests ───────────────────────────────────────────────────────────
60
+
61
+ describe('Production sort: getMemberBySlug must return the HIGHEST counter', () => {
62
+ test('should return -12 as the highest URL (not -9)', () => {
63
+ const members = buildMembersInDb();
64
+ const highest = simulateGetHighestMember(members, 'firstnamelastname');
65
+
66
+ expect(highest.url).toBe('firstNameLastName-12');
67
+ });
68
+
69
+ test('should sort -12 above -9 in descending order', () => {
70
+ const members = buildMembersInDb();
71
+ const sorted = [...members]
72
+ .filter(m => normalizeUrlForComparison(m.url) === 'firstnamelastname')
73
+ .sort(sortByUrlCounterDescending);
74
+ const sortedUrls = sorted.map(m => m.url);
75
+
76
+ const indexOf9 = sortedUrls.indexOf('firstNameLastName-9');
77
+ const indexOf12 = sortedUrls.indexOf('firstNameLastName-12');
78
+
79
+ expect(indexOf12).toBeLessThan(indexOf9);
80
+ });
81
+
82
+ test('should handle large counters (100+) correctly', () => {
83
+ const members = buildLargeCounterMembers();
84
+ const highest = simulateGetHighestMember(members, 'testuser');
85
+
86
+ expect(highest.url).toBe('testUser-105');
87
+ });
88
+
89
+ test('should handle hyphenated names (mary-jane) correctly', () => {
90
+ const members = [
91
+ { memberId: 1, url: 'mary-jane' },
92
+ { memberId: 2, url: 'mary-jane-1' },
93
+ { memberId: 3, url: 'mary-jane-2' },
94
+ { memberId: 4, url: 'mary-jane-10' },
95
+ ];
96
+
97
+ const highest = simulateGetHighestMember(members, 'mary-jane');
98
+ expect(highest.url).toBe('mary-jane-10');
99
+ });
100
+ });
101
+
102
+ describe('Production sort: ensureUniqueUrl must generate a truly unique URL', () => {
103
+ test('should generate -13 (not -10) when DB has URLs up to -12', () => {
104
+ const members = buildMembersInDb();
105
+ const highest = simulateGetHighestMember(members, 'firstnamelastname');
106
+ const newUrl = simulateEnsureUniqueUrl('firstNameLastName', highest);
107
+
108
+ expect(newUrl).toBe('firstNameLastName-13');
109
+
110
+ const existingUrls = members.map(m => m.url);
111
+ expect(existingUrls).not.toContain(newUrl);
112
+ });
113
+
114
+ test('repeated daily pulls should produce sequential unique URLs', () => {
115
+ const members = buildMembersInDb();
116
+ const generatedUrls = [];
117
+
118
+ for (let day = 0; day < 5; day++) {
119
+ const highest = simulateGetHighestMember(members, 'firstnamelastname');
120
+ const newUrl = simulateEnsureUniqueUrl('firstNameLastName', highest);
121
+ generatedUrls.push(newUrl);
122
+ members.push({ memberId: 100 + day, url: newUrl });
123
+ }
124
+
125
+ expect(new Set(generatedUrls).size).toBe(5);
126
+ expect(generatedUrls).toEqual([
127
+ 'firstNameLastName-13',
128
+ 'firstNameLastName-14',
129
+ 'firstNameLastName-15',
130
+ 'firstNameLastName-16',
131
+ 'firstNameLastName-17',
132
+ ]);
133
+ });
134
+
135
+ test('should NOT produce duplicates (the -10 bug)', () => {
136
+ const members = buildMembersInDb();
137
+ const highest = simulateGetHighestMember(members, 'firstnamelastname');
138
+ const newUrl = simulateEnsureUniqueUrl('firstNameLastName', highest);
139
+
140
+ expect(newUrl).not.toBe('firstNameLastName-10');
141
+ });
142
+ });
143
+
144
+ describe('Production sort: ensureUniqueUrlsInBatch single-member path', () => {
145
+ test('incrementUrlCounter should produce a unique URL from highest DB member', () => {
146
+ const members = buildMembersInDb();
147
+ const dbMember = simulateGetHighestMember(members, 'firstnamelastname');
148
+ const result = incrementUrlCounter(dbMember.url, 'firstNameLastName');
149
+
150
+ expect(result).toBe('firstNameLastName-13');
151
+
152
+ const existingUrls = members.map(m => m.url);
153
+ expect(existingUrls).not.toContain(result);
154
+ });
155
+ });
156
+
157
+ describe('Utility functions', () => {
158
+ test('normalizeUrlForComparison strips trailing counter', () => {
159
+ expect(normalizeUrlForComparison('firstNameLastName')).toBe('firstnamelastname');
160
+ expect(normalizeUrlForComparison('firstNameLastName-1')).toBe('firstnamelastname');
161
+ expect(normalizeUrlForComparison('firstNameLastName-9')).toBe('firstnamelastname');
162
+ expect(normalizeUrlForComparison('firstNameLastName-10')).toBe('firstnamelastname');
163
+ expect(normalizeUrlForComparison('firstNameLastName-100')).toBe('firstnamelastname');
164
+ });
165
+
166
+ test('extractUrlCounter returns numeric counter', () => {
167
+ expect(extractUrlCounter('firstNameLastName')).toBe(-1);
168
+ expect(extractUrlCounter('firstNameLastName-1')).toBe(1);
169
+ expect(extractUrlCounter('firstNameLastName-9')).toBe(9);
170
+ expect(extractUrlCounter('firstNameLastName-10')).toBe(10);
171
+ expect(extractUrlCounter('firstNameLastName-100')).toBe(100);
172
+ expect(extractUrlCounter(null)).toBe(-1);
173
+ expect(extractUrlCounter('')).toBe(-1);
174
+ });
175
+
176
+ test('extractBaseUrl strips numeric counter', () => {
177
+ expect(extractBaseUrl('firstNameLastName-10')).toBe('firstNameLastName');
178
+ expect(extractBaseUrl('firstNameLastName-1')).toBe('firstNameLastName');
179
+ expect(extractBaseUrl('firstNameLastName')).toBe('firstNameLastName');
180
+ expect(extractBaseUrl('john-doe-3')).toBe('john-doe');
181
+ });
182
+
183
+ test('incrementUrlCounter increments correctly', () => {
184
+ expect(incrementUrlCounter('firstNameLastName-9', 'firstNameLastName')).toBe(
185
+ 'firstNameLastName-10'
186
+ );
187
+ expect(incrementUrlCounter('firstNameLastName-10', 'firstNameLastName')).toBe(
188
+ 'firstNameLastName-11'
189
+ );
190
+ expect(incrementUrlCounter('firstNameLastName', 'firstNameLastName')).toBe(
191
+ 'firstNameLastName-1'
192
+ );
193
+ });
194
+ });
@@ -148,7 +148,9 @@ function buildMembersSearchQuery(data) {
148
148
  latitude: filter.latitude,
149
149
  longitude: filter.longitude,
150
150
  },
151
- findMainAddress(item.addressDisplayOption, item.addresses)
151
+ findMainAddress(item.addressDisplayOption, item.addresses, {
152
+ requireValidCoordinates: true,
153
+ })
152
154
  ),
153
155
  }));
154
156
  const resultWithDistances = {
@@ -1,12 +1,8 @@
1
1
  const { bulkSaveMembers, getMemberBySlug } = require('../members-data-methods');
2
+ const { extractUrlCounter } = require('../utils');
2
3
 
3
4
  const { generateUpdatedMemberData } = require('./process-member-methods');
4
- const {
5
- changeWixMembersEmails,
6
- extractUrlCounter,
7
- incrementUrlCounter,
8
- extractBaseUrl,
9
- } = require('./utils');
5
+ const { changeWixMembersEmails, incrementUrlCounter, extractBaseUrl } = require('./utils');
10
6
 
11
7
  /**
12
8
  * Ensures unique URLs within a batch of members by deduplicating URLs
@@ -20,7 +16,8 @@ async function ensureUniqueUrlsInBatch(memberDataList) {
20
16
  return memberDataList;
21
17
  }
22
18
 
23
- // Group members by their normalized base URL
19
+ // Group members by their normalized base URL (case-insensitive to avoid
20
+ // "John-12" and "john-12" being treated as different when slugs are matched case-insensitively)
24
21
  const urlGroups = new Map();
25
22
 
26
23
  memberDataList.forEach(member => {
@@ -28,16 +25,17 @@ async function ensureUniqueUrlsInBatch(memberDataList) {
28
25
  return;
29
26
  }
30
27
 
31
- // Extract the base URL (without any counter) for grouping
32
28
  const baseUrl = extractBaseUrl(member.url);
33
- if (!urlGroups.has(baseUrl)) {
34
- urlGroups.set(baseUrl, []);
29
+ const groupKey = baseUrl.toLowerCase();
30
+ if (!urlGroups.has(groupKey)) {
31
+ urlGroups.set(groupKey, []);
35
32
  }
36
- urlGroups.get(baseUrl).push(member);
33
+ urlGroups.get(groupKey).push(member);
37
34
  });
38
35
 
39
36
  // For each group, check database and assign unique URLs sequentially
40
- for (const [baseUrl, members] of urlGroups.entries()) {
37
+ for (const [groupKey, members] of urlGroups.entries()) {
38
+ const baseUrl = groupKey; // lowercase for consistent slug assignment
41
39
  if (members.length <= 1) {
42
40
  // Single member - still check DB to ensure it doesn't conflict with other pages
43
41
  const member = members[0];
@@ -80,14 +78,9 @@ async function ensureUniqueUrlsInBatch(memberDataList) {
80
78
  let batchMaxCounter = -1;
81
79
  members.forEach(member => {
82
80
  const originalUrl = member.url;
83
- const urlParts = originalUrl.split('-');
84
- const lastSegment = urlParts[urlParts.length - 1];
85
- const isNumeric = /^\d+$/.test(lastSegment);
86
- if (isNumeric) {
87
- const counter = parseInt(lastSegment, 10);
88
- if (counter > batchMaxCounter) {
89
- batchMaxCounter = counter;
90
- }
81
+ const urlCounter = extractUrlCounter(originalUrl);
82
+ if (urlCounter > batchMaxCounter) {
83
+ batchMaxCounter = urlCounter;
91
84
  }
92
85
  });
93
86
 
@@ -3,6 +3,7 @@ const { findMemberById, getMemberBySlug } = require('../members-data-methods');
3
3
  const { isValidArray, generateGeoHash } = require('../utils');
4
4
 
5
5
  const { MEMBER_ACTIONS, DEFAULT_MEMBER_DISPLAY_SETTINGS } = require('./consts');
6
+ const { incrementUrlCounter } = require('./utils');
6
7
  const { validateCoreMemberData, containsNonEnglish, createFullName } = require('./utils');
7
8
 
8
9
  /**
@@ -44,9 +45,7 @@ const ensureUniqueUrl = async ({ url, memberId, fullName }) => {
44
45
  console.log(
45
46
  `Found member with same url ${existingMember.url} for memberId ${memberId} and URL ${uniqueUrl}, increasing counter by 1`
46
47
  );
47
- const lastSegment = existingMember.url.split('-').pop() || '0';
48
- const lastCounter = parseInt(lastSegment, 10) || 0;
49
- uniqueUrl = `${uniqueUrl}-${lastCounter + 1}`;
48
+ uniqueUrl = incrementUrlCounter(existingMember.url, uniqueUrl);
50
49
  }
51
50
  if (uniqueUrl !== baseUrl) {
52
51
  console.log(`URL conflict resolved: ${baseUrl} -> ${uniqueUrl} for member ${memberId}`);
@@ -1,4 +1,5 @@
1
1
  const { updateWixMemberLoginEmail } = require('../members-area-methods');
2
+ const { extractUrlCounter } = require('../utils');
2
3
 
3
4
  const { MEMBER_ACTIONS } = require('./consts');
4
5
 
@@ -15,21 +16,12 @@ const changeWixMembersEmails = async toChangeWixMembersEmails => {
15
16
  );
16
17
  };
17
18
 
18
- const extractUrlCounter = url => {
19
- if (!url) return -1;
20
- const lastSegment = url.split('-').pop() || '0';
21
- const isNumeric = /^\d+$/.test(lastSegment);
22
- return isNumeric ? parseInt(lastSegment, 10) : -1;
23
- };
24
-
25
19
  const extractBaseUrl = url => {
26
20
  if (!url) return url;
27
- const urlParts = url.split('-');
28
- const lastSegment = urlParts[urlParts.length - 1];
29
- const isNumeric = /^\d+$/.test(lastSegment);
30
- if (isNumeric && urlParts.length > 1) {
21
+ const lastCounter = extractUrlCounter(url);
22
+ if (lastCounter > 0) {
31
23
  // Remove the numeric counter to get the base URL
32
- return urlParts.slice(0, -1).join('-');
24
+ return url.split('-').slice(0, -1).join('-');
33
25
  }
34
26
  // No counter found, return the URL as-is
35
27
  return url;
@@ -49,9 +41,7 @@ const incrementUrlCounter = (existingUrl, baseUrl) => {
49
41
  console.log(
50
42
  `Found member with same url ${existingUrl} for baseUrl ${baseUrl}, increasing counter by 1`
51
43
  );
52
- const lastSegment = existingUrl.split('-').pop() || '0';
53
- const isNumeric = /^\d+$/.test(lastSegment);
54
- const lastCounter = isNumeric ? parseInt(lastSegment, 10) : 0;
44
+ const lastCounter = Math.max(0, extractUrlCounter(existingUrl));
55
45
  return `${baseUrl}-${lastCounter + 1}`;
56
46
  }
57
47
 
@@ -118,7 +108,6 @@ module.exports = {
118
108
  validateCoreMemberData,
119
109
  containsNonEnglish,
120
110
  createFullName,
121
- extractUrlCounter,
122
111
  incrementUrlCounter,
123
112
  extractBaseUrl,
124
113
  };
@@ -1,6 +1,7 @@
1
1
  const { COLLECTIONS } = require('../public/consts');
2
2
 
3
3
  const { ensureUniqueUrlsInBatch } = require('./daily-pull/bulk-process-methods');
4
+ const { ensureUniqueUrl } = require('./daily-pull/process-member-methods');
4
5
  const { wixData } = require('./elevated-modules');
5
6
  const { bulkSaveMembers } = require('./members-data-methods');
6
7
  const { queryAllItems } = require('./utils');
@@ -27,4 +28,31 @@ async function copyContactIdToWixMemberId() {
27
28
  return await bulkSaveMembers(updatedMembers, COLLECTIONS.MEMBERS_DATA);
28
29
  }
29
30
 
30
- module.exports = { deduplicateURls, copyContactIdToWixMemberId };
31
+ async function createMissingUrls() {
32
+ const query = wixData.query(COLLECTIONS.MEMBERS_DATA).isEmpty('url');
33
+ const members = await queryAllItems(query);
34
+ console.log(
35
+ 'membersWithoutUrls info',
36
+ JSON.stringify({
37
+ count: members.length,
38
+ membersIds: members.map(m => m.memberId),
39
+ })
40
+ );
41
+
42
+ const membersWithGeneratedUrlsPromises = members.map(async member => ({
43
+ ...member,
44
+ url: await ensureUniqueUrl({
45
+ url: member.url,
46
+ memberId: member.memberId,
47
+ fullName: member.fullName,
48
+ }),
49
+ }));
50
+ const membersWithGeneratedUrls = await Promise.all(membersWithGeneratedUrlsPromises);
51
+ //recheck urls in same batch to avoid duplicates
52
+ const uniqueUrlsUpdatedMembers = await ensureUniqueUrlsInBatch(membersWithGeneratedUrls);
53
+ const urls = uniqueUrlsUpdatedMembers.map(m => m.url).filter(Boolean);
54
+ console.log('unique urls', [...new Set(urls)]);
55
+ return await bulkSaveMembers(uniqueUrlsUpdatedMembers, COLLECTIONS.MEMBERS_DATA);
56
+ }
57
+
58
+ module.exports = { deduplicateURls, copyContactIdToWixMemberId, createMissingUrls };
@@ -10,6 +10,7 @@ const { createSiteMember, getCurrentMember } = require('./members-area-methods')
10
10
  const {
11
11
  chunkArray,
12
12
  normalizeUrlForComparison,
13
+ sortByUrlCounterDescending,
13
14
  queryAllItems,
14
15
  generateGeoHash,
15
16
  searchAllItems,
@@ -176,7 +177,7 @@ async function getMemberBySlug({
176
177
  //remove trailing "-1", "-2", etc.
177
178
  item => item.url && normalizeUrlForComparison(item.url) === slug.toLowerCase()
178
179
  )
179
- .sort((a, b) => b.url.toLowerCase().localeCompare(a.url.toLowerCase()));
180
+ .sort(sortByUrlCounterDescending);
180
181
  }
181
182
  if (matchingMembers.length > 1) {
182
183
  const queryResultMsg = `Multiple members found with same slug ${slug} membersIds are : [${matchingMembers
package/backend/utils.js CHANGED
@@ -164,6 +164,14 @@ const normalizeUrlForComparison = url => {
164
164
  return url.toLowerCase().replace(/-\d+$/, '');
165
165
  };
166
166
 
167
+ const extractUrlCounter = url => {
168
+ if (!url) return -1;
169
+ const lastSegment = url.split('-').pop() || '0';
170
+ return /^\d+$/.test(lastSegment) ? parseInt(lastSegment, 10) : -1;
171
+ };
172
+
173
+ const sortByUrlCounterDescending = (a, b) => extractUrlCounter(b.url) - extractUrlCounter(a.url);
174
+
167
175
  async function getSecret(secretKey) {
168
176
  return (await elevatedGetSecretValue(secretKey)).value;
169
177
  }
@@ -209,6 +217,8 @@ module.exports = {
209
217
  generateGeoHash,
210
218
  isValidArray,
211
219
  normalizeUrlForComparison,
220
+ sortByUrlCounterDescending,
221
+ extractUrlCounter,
212
222
  queryAllItems,
213
223
  formatDateToMonthYear,
214
224
  isStudent,
@@ -0,0 +1,201 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const csv = require('csv-parser');
5
+
6
+ /**
7
+ * Reads a Members Data CSV and extracts groups of non-unique URLs:
8
+ * each group is a URL plus the list of IDs and memberIds that share that URL.
9
+ * Only outputs groups where the same URL appears more than once.
10
+ * Report includes duplicateUrls, memberIdsWithDuplicateUrls (flat array of unique memberIds
11
+ * in duplicate groups), and nonUniqueMemberIds (memberIds that appear more than once, with count).
12
+ *
13
+ * Usage: node dev-only-scripts/extract-duplicate-url-groups.js <path-to-csv> [output-json-path]
14
+ * Example: node dev-only-scripts/extract-duplicate-url-groups.js "/Users/Besan/Downloads/Members+Data+Latest (17).csv"
15
+ *
16
+ * CSV must have columns: "url", "ID", and "memberId" (case-insensitive).
17
+ */
18
+ function extractDuplicateUrlGroups(csvFilePath, outputJsonPath) {
19
+ if (!csvFilePath) {
20
+ console.error('Error: CSV file path is required');
21
+ console.error(
22
+ 'Usage: node dev-only-scripts/extract-duplicate-url-groups.js <path-to-csv> [output-json-path]'
23
+ );
24
+ process.exit(1);
25
+ }
26
+
27
+ if (!fs.existsSync(csvFilePath)) {
28
+ console.error(`Error: File not found: ${csvFilePath}`);
29
+ process.exit(1);
30
+ }
31
+
32
+ const urlToRows = new Map(); // url -> [{ id, memberId }, ...]
33
+ let totalRows = 0;
34
+ let headersValidated = false;
35
+ let headers = null;
36
+ let urlColumnName = null;
37
+ let idColumnName = null;
38
+ let memberIdColumnName = null;
39
+
40
+ return new Promise((resolve, reject) => {
41
+ fs.createReadStream(csvFilePath)
42
+ .pipe(csv())
43
+ .on('headers', receivedHeaders => {
44
+ headers = receivedHeaders;
45
+ const normalizedHeaders = headers.map(h => {
46
+ const normalized = String(h).trim().replace(/["']/g, '');
47
+ return normalized.toLowerCase().trim();
48
+ });
49
+
50
+ const urlIndex = normalizedHeaders.indexOf('url');
51
+ const idIndex = normalizedHeaders.indexOf('id');
52
+ const memberIdIndex = normalizedHeaders.indexOf('memberid');
53
+
54
+ if (urlIndex === -1 || idIndex === -1 || memberIdIndex === -1) {
55
+ console.error(
56
+ 'Error: CSV must contain "url", "ID", and "memberId" columns (case-insensitive)'
57
+ );
58
+ console.error(`Found columns: ${headers.join(', ')}`);
59
+ process.exit(1);
60
+ }
61
+
62
+ urlColumnName = headers[urlIndex];
63
+ idColumnName = headers[idIndex];
64
+ memberIdColumnName = headers[memberIdIndex];
65
+ headersValidated = true;
66
+ })
67
+ .on('data', row => {
68
+ if (!headersValidated) {
69
+ headers = Object.keys(row);
70
+ const normalizedHeaders = headers.map(h => {
71
+ const normalized = String(h).trim().replace(/["']/g, '');
72
+ return normalized.toLowerCase().trim();
73
+ });
74
+ const urlIndex = normalizedHeaders.indexOf('url');
75
+ const idIndex = normalizedHeaders.indexOf('id');
76
+ const memberIdIndex = normalizedHeaders.indexOf('memberid');
77
+ if (urlIndex === -1 || idIndex === -1 || memberIdIndex === -1) {
78
+ console.error(
79
+ 'Error: CSV must contain "url", "ID", and "memberId" columns (case-insensitive)'
80
+ );
81
+ process.exit(1);
82
+ }
83
+ urlColumnName = headers[urlIndex];
84
+ idColumnName = headers[idIndex];
85
+ memberIdColumnName = headers[memberIdIndex];
86
+ headersValidated = true;
87
+ }
88
+
89
+ totalRows++;
90
+
91
+ const url = row[urlColumnName];
92
+ const id = row[idColumnName];
93
+ const memberId = row[memberIdColumnName];
94
+
95
+ if (!url || !id) {
96
+ return;
97
+ }
98
+
99
+ const trimmedUrl = url.trim();
100
+ const trimmedId = id.trim();
101
+ const trimmedMemberId =
102
+ memberId != null && String(memberId).trim() !== '' ? String(memberId).trim() : null;
103
+
104
+ if (!urlToRows.has(trimmedUrl)) {
105
+ urlToRows.set(trimmedUrl, []);
106
+ }
107
+ urlToRows.get(trimmedUrl).push({ id: trimmedId, memberId: trimmedMemberId });
108
+ })
109
+ .on('error', error => {
110
+ console.error('Error reading CSV file:', error.message);
111
+ reject(error);
112
+ })
113
+ .on('end', () => {
114
+ if (!headersValidated) {
115
+ console.error('Error: Could not read CSV headers');
116
+ process.exit(1);
117
+ }
118
+
119
+ const groups = [];
120
+ for (const [url, rows] of urlToRows.entries()) {
121
+ if (rows.length > 1) {
122
+ const ids = rows.map(r => r.id);
123
+ const memberIds = rows.map(r => r.memberId).filter(Boolean);
124
+ groups.push({ url, ids, memberIds });
125
+ }
126
+ }
127
+
128
+ groups.sort((a, b) => {
129
+ if (b.ids.length !== a.ids.length) return b.ids.length - a.ids.length;
130
+ return a.url.localeCompare(b.url);
131
+ });
132
+
133
+ const totalIdsInGroups = groups.reduce((sum, g) => sum + g.ids.length, 0);
134
+ const duplicateUrls = groups.map(g => g.url);
135
+ const allMemberIdOccurrences = groups.flatMap(g => g.memberIds);
136
+ const memberIdsWithDuplicateUrls = [...new Set(allMemberIdOccurrences)];
137
+
138
+ const memberIdCounts = new Map();
139
+ for (const memberId of allMemberIdOccurrences) {
140
+ memberIdCounts.set(memberId, (memberIdCounts.get(memberId) || 0) + 1);
141
+ }
142
+ const nonUniqueMemberIds = [...memberIdCounts.entries()]
143
+ .filter(([, count]) => count > 1)
144
+ .map(([memberId, count]) => ({ memberId, count }))
145
+ .sort((a, b) => b.count - a.count);
146
+
147
+ const report = {
148
+ totalRowsProcessed: totalRows,
149
+ totalDuplicateGroups: groups.length,
150
+ totalIdsInDuplicateGroups: totalIdsInGroups,
151
+ totalMemberIdOccurrencesInDuplicateGroups: allMemberIdOccurrences.length,
152
+ uniqueMemberIdsWithDuplicateUrls: memberIdsWithDuplicateUrls.length,
153
+ nonUniqueMemberIdsCount: nonUniqueMemberIds.length,
154
+ duplicateUrls,
155
+ memberIdsWithDuplicateUrls,
156
+ nonUniqueMemberIds,
157
+ groups,
158
+ };
159
+
160
+ if (outputJsonPath) {
161
+ fs.writeFileSync(outputJsonPath, JSON.stringify(report, null, 2), 'utf8');
162
+ console.log(`Report written to: ${outputJsonPath}`);
163
+ } else {
164
+ const csvDir = path.dirname(csvFilePath);
165
+ const csvBasename = path.basename(csvFilePath, path.extname(csvFilePath));
166
+ const defaultPath = path.join(csvDir, `${csvBasename}-duplicate-url-groups.json`);
167
+ fs.writeFileSync(defaultPath, JSON.stringify(report, null, 2), 'utf8');
168
+ console.log(`Report written to: ${defaultPath}`);
169
+ }
170
+
171
+ console.log('\n=== Non-unique URL groups (same URL → list of IDs, memberIds) ===');
172
+ console.log(`Total rows processed: ${totalRows}`);
173
+ console.log(`Number of duplicate URL groups: ${groups.length}`);
174
+ console.log(`Total IDs in those groups: ${totalIdsInGroups}`);
175
+ console.log(`Total memberId occurrences in those groups: ${allMemberIdOccurrences.length}`);
176
+ console.log(`Unique memberIds in those groups: ${memberIdsWithDuplicateUrls.length}`);
177
+ console.log(
178
+ `MemberIds that appear more than once (non-unique): ${nonUniqueMemberIds.length}`
179
+ );
180
+ if (duplicateUrls.length > 0) {
181
+ console.log('\nFirst 15 duplicate URLs:');
182
+ duplicateUrls.slice(0, 15).forEach((url, i) => {
183
+ console.log(` ${i + 1}. ${url}`);
184
+ });
185
+ }
186
+
187
+ resolve(report);
188
+ });
189
+ });
190
+ }
191
+
192
+ if (require.main === module) {
193
+ const csvFilePath = process.argv[2];
194
+ const outputJsonPath = process.argv[3];
195
+ extractDuplicateUrlGroups(csvFilePath, outputJsonPath).catch(error => {
196
+ console.error('Fatal error:', error.message);
197
+ process.exit(1);
198
+ });
199
+ }
200
+
201
+ module.exports = { extractDuplicateUrlGroups };
@@ -0,0 +1,159 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const csv = require('csv-parser');
5
+
6
+ /**
7
+ * Reads a Members Data CSV and finds duplicate memberIds: any memberId (memberId column)
8
+ * that appears in more than one row. Outputs a JSON report with the list of
9
+ * duplicate memberIds and, per memberId, the rows (e.g. ID, url) where it appears.
10
+ *
11
+ * Usage: node dev-only-scripts/find-duplicate-ids.js <path-to-csv> [output-json-path]
12
+ *
13
+ * CSV must have a "memberId" column (case-insensitive). Optional: "ID", "url" for row details.
14
+ */
15
+ function findDuplicateMemberIds(csvFilePath, outputJsonPath) {
16
+ if (!csvFilePath) {
17
+ console.error('Error: CSV file path is required');
18
+ console.error(
19
+ 'Usage: node dev-only-scripts/find-duplicate-ids.js <path-to-csv> [output-json-path]'
20
+ );
21
+ process.exit(1);
22
+ }
23
+
24
+ if (!fs.existsSync(csvFilePath)) {
25
+ console.error(`Error: File not found: ${csvFilePath}`);
26
+ process.exit(1);
27
+ }
28
+
29
+ const memberIdToRows = new Map(); // memberId -> [{ rowNumber, id?, url? }, ...]
30
+ let totalRows = 0;
31
+ let headersValidated = false;
32
+ let headers = null;
33
+ let memberIdColumnName = null;
34
+ let idColumnName = null;
35
+ let urlColumnName = null;
36
+
37
+ return new Promise((resolve, reject) => {
38
+ fs.createReadStream(csvFilePath)
39
+ .pipe(csv())
40
+ .on('headers', receivedHeaders => {
41
+ headers = receivedHeaders;
42
+ const normalizedHeaders = headers.map(h => {
43
+ const normalized = String(h).trim().replace(/["']/g, '');
44
+ return normalized.toLowerCase().trim();
45
+ });
46
+
47
+ const memberIdIndex = normalizedHeaders.indexOf('memberid');
48
+ if (memberIdIndex === -1) {
49
+ console.error('Error: CSV must contain a "memberId" column (case-insensitive)');
50
+ console.error(`Found columns: ${headers.join(', ')}`);
51
+ process.exit(1);
52
+ }
53
+
54
+ memberIdColumnName = headers[memberIdIndex];
55
+ idColumnName = headers[normalizedHeaders.indexOf('id')] || null;
56
+ urlColumnName = headers[normalizedHeaders.indexOf('url')] || null;
57
+ headersValidated = true;
58
+ })
59
+ .on('data', row => {
60
+ if (!headersValidated) {
61
+ headers = Object.keys(row);
62
+ const normalizedHeaders = headers.map(h => {
63
+ const normalized = String(h).trim().replace(/["']/g, '');
64
+ return normalized.toLowerCase().trim();
65
+ });
66
+ const memberIdIndex = normalizedHeaders.indexOf('memberid');
67
+ if (memberIdIndex === -1) {
68
+ console.error('Error: CSV must contain a "memberId" column (case-insensitive)');
69
+ process.exit(1);
70
+ }
71
+ memberIdColumnName = headers[memberIdIndex];
72
+ idColumnName = headers[normalizedHeaders.indexOf('id')] || null;
73
+ urlColumnName = headers[normalizedHeaders.indexOf('url')] || null;
74
+ headersValidated = true;
75
+ }
76
+
77
+ totalRows++;
78
+ const memberId = row[memberIdColumnName];
79
+ if (memberId == null || String(memberId).trim() === '') return;
80
+
81
+ const trimmedMemberId = String(memberId).trim();
82
+ const rowInfo = { rowNumber: totalRows };
83
+ if (idColumnName && row[idColumnName] != null)
84
+ rowInfo.id = String(row[idColumnName]).trim();
85
+ if (urlColumnName && row[urlColumnName] != null)
86
+ rowInfo.url = String(row[urlColumnName]).trim();
87
+
88
+ if (!memberIdToRows.has(trimmedMemberId)) {
89
+ memberIdToRows.set(trimmedMemberId, []);
90
+ }
91
+ memberIdToRows.get(trimmedMemberId).push(rowInfo);
92
+ })
93
+ .on('error', error => {
94
+ console.error('Error reading CSV file:', error.message);
95
+ reject(error);
96
+ })
97
+ .on('end', () => {
98
+ if (!headersValidated) {
99
+ console.error('Error: Could not read CSV headers');
100
+ process.exit(1);
101
+ }
102
+
103
+ const groups = [];
104
+ for (const [memberId, rows] of memberIdToRows.entries()) {
105
+ if (rows.length > 1) {
106
+ groups.push({ memberId, count: rows.length, rows });
107
+ }
108
+ }
109
+
110
+ groups.sort((a, b) => b.count - a.count);
111
+
112
+ const duplicateMemberIds = groups.map(g => g.memberId);
113
+ const totalDuplicateRows = groups.reduce((sum, g) => sum + g.count, 0);
114
+
115
+ const report = {
116
+ totalRowsProcessed: totalRows,
117
+ totalDuplicateMemberIds: groups.length,
118
+ totalRowsWithDuplicateMemberIds: totalDuplicateRows,
119
+ duplicateMemberIds,
120
+ groups,
121
+ };
122
+
123
+ if (outputJsonPath) {
124
+ fs.writeFileSync(outputJsonPath, JSON.stringify(report, null, 2), 'utf8');
125
+ console.log(`Report written to: ${outputJsonPath}`);
126
+ } else {
127
+ const csvDir = path.dirname(csvFilePath);
128
+ const csvBasename = path.basename(csvFilePath, path.extname(csvFilePath));
129
+ const defaultPath = path.join(csvDir, `${csvBasename}-duplicate-member-ids-report.json`);
130
+ fs.writeFileSync(defaultPath, JSON.stringify(report, null, 2), 'utf8');
131
+ console.log(`Report written to: ${defaultPath}`);
132
+ }
133
+
134
+ console.log('\n=== Duplicate memberIds Report ===');
135
+ console.log(`Total rows processed: ${totalRows}`);
136
+ console.log(`memberIds that appear more than once: ${groups.length}`);
137
+ console.log(`Total rows with those memberIds: ${totalDuplicateRows}`);
138
+ if (groups.length > 0) {
139
+ console.log('\nDuplicate memberIds (first 20):');
140
+ groups.slice(0, 20).forEach((g, i) => {
141
+ console.log(` ${i + 1}. ${g.memberId} (${g.count} rows)`);
142
+ });
143
+ }
144
+
145
+ resolve(report);
146
+ });
147
+ });
148
+ }
149
+
150
+ if (require.main === module) {
151
+ const csvFilePath = process.argv[2];
152
+ const outputJsonPath = process.argv[3];
153
+ findDuplicateMemberIds(csvFilePath, outputJsonPath).catch(error => {
154
+ console.error('Fatal error:', error.message);
155
+ process.exit(1);
156
+ });
157
+ }
158
+
159
+ module.exports = { findDuplicateMemberIds };
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "abmp-npm",
3
- "version": "10.0.55",
3
+ "version": "10.0.57",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "check-cycles": "madge --circular .",
7
- "test": "echo \"Error: no test specified\" && exit 1",
7
+ "test": "jest backend/__tests__",
8
8
  "lint": "npm run check-cycles && eslint .",
9
9
  "lint:fix": "eslint . --fix",
10
10
  "format": "prettier --write \"**/*.{js,json,md}\"",
11
11
  "format:check": "prettier --check \"**/*.{js,json,md}\"",
12
12
  "prepare": "husky",
13
- "find-duplicates": "node dev-only-scripts/find-duplicate-urls.js"
13
+ "find-duplicates": "node dev-only-scripts/find-duplicate-urls.js",
14
+ "extract-duplicate-url-groups": "node dev-only-scripts/extract-duplicate-url-groups.js",
15
+ "find-duplicate-ids": "node dev-only-scripts/find-duplicate-ids.js"
14
16
  },
15
17
  "author": "",
16
18
  "license": "ISC",
@@ -25,6 +27,7 @@
25
27
  "eslint-plugin-promise": "^7.1.0",
26
28
  "globals": "^15.10.0",
27
29
  "husky": "^9.1.6",
30
+ "jest": "^30.3.0",
28
31
  "madge": "^8.0.0",
29
32
  "prettier": "^3.3.3"
30
33
  },
@@ -45,9 +48,9 @@
45
48
  "aws4": "^1.13.2",
46
49
  "axios": "^1.13.1",
47
50
  "crypto": "^1.0.1",
51
+ "csv-parser": "^3.0.0",
48
52
  "jwt-js-decode": "^1.9.0",
49
53
  "lodash": "^4.17.21",
50
- "csv-parser": "^3.0.0",
51
54
  "ngeohash": "^0.6.3",
52
55
  "phone": "^3.1.67",
53
56
  "psdev-task-manager": "1.1.10",
@@ -66,20 +66,31 @@ function debouncedFunction({ func, debounceTimeout, timeoutType, args }) {
66
66
 
67
67
  const isValidLocation = location => location.latitude && location.longitude;
68
68
 
69
- function findMainAddress(addressDisplayOption = [], addresses = []) {
70
- const options = Array.isArray(addressDisplayOption) ? addressDisplayOption : [];
71
- const mainOpt = options.find(opt => opt.isMain);
69
+ /**
70
+ * @param {Array} addressDisplayOption
71
+ * @param {Array} addresses
72
+ * @param {Object|boolean} [options] - Optional. Pass { requireValidCoordinates: true } for home search/distance; omit or false for profile display.
73
+ */
74
+ function findMainAddress(addressDisplayOption = [], addresses = [], options = {}) {
75
+ const requireValidCoordinates =
76
+ typeof options === 'boolean' ? options : Boolean(options?.requireValidCoordinates);
77
+ const optionsArr = Array.isArray(addressDisplayOption) ? addressDisplayOption : [];
78
+ const mainOpt = optionsArr.find(opt => opt.isMain);
72
79
  if (mainOpt) {
73
80
  const mainAddr = addresses.find(
74
- addr => addr.key === mainOpt.key && addr.addressStatus !== ADDRESS_STATUS_TYPES.DONT_SHOW
81
+ addr =>
82
+ addr.key === mainOpt.key &&
83
+ addr.addressStatus !== ADDRESS_STATUS_TYPES.DONT_SHOW &&
84
+ (!requireValidCoordinates || isValidLocation(addr))
75
85
  );
76
- if (mainAddr && isValidLocation(mainAddr)) {
86
+ if (mainAddr) {
77
87
  return mainAddr;
78
88
  }
79
89
  }
80
- // 2) fallback: if there is any visible address, use it
81
90
  const visibleAddresses = addresses.filter(
82
- addr => addr.addressStatus !== ADDRESS_STATUS_TYPES.DONT_SHOW && isValidLocation(addr)
91
+ addr =>
92
+ addr.addressStatus !== ADDRESS_STATUS_TYPES.DONT_SHOW &&
93
+ (!requireValidCoordinates || isValidLocation(addr))
83
94
  );
84
95
  if (visibleAddresses.length) {
85
96
  return visibleAddresses[0];
@@ -107,8 +118,13 @@ function formatAddress(item) {
107
118
  return addressParts.filter(Boolean).join(', ');
108
119
  }
109
120
 
110
- function getMainAddress(addressDisplayOption = [], addresses = []) {
111
- const mainAddr = findMainAddress(addressDisplayOption, addresses);
121
+ /**
122
+ * @param {Array} addressDisplayOption
123
+ * @param {Array} addresses
124
+ * @param {Object|boolean} [options] - Optional. Pass { requireValidCoordinates: true } for home search/distance; omit or false for profile display.
125
+ */
126
+ function getMainAddress(addressDisplayOption = [], addresses = [], options = {}) {
127
+ const mainAddr = findMainAddress(addressDisplayOption, addresses, options);
112
128
  if (mainAddr) {
113
129
  return formatAddress(mainAddr);
114
130
  }