Skip to content

Commit faaf5ec

Browse files
committed
mg-beehiiv-members: Add tiers as labels
1 parent 131ed5d commit faaf5ec

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

packages/mg-beehiiv-members/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@tryghost/debug": "0.1.35",
3838
"@tryghost/errors": "1.3.8",
39-
"@tryghost/mg-fs-utils": "0.12.14"
39+
"@tryghost/mg-fs-utils": "0.12.14",
40+
"@tryghost/string": "0.2.17"
4041
}
4142
}

packages/mg-beehiiv-members/src/lib/process.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fsUtils from '@tryghost/mg-fs-utils';
2+
import {slugify} from '@tryghost/string';
23

34
type memberObject = {
45
email: string;
@@ -47,7 +48,6 @@ const processCsv = async ({csvPath}: {csvPath: string}) => {
4748
};
4849

4950
// First Name,Last Name
50-
5151
if (member['First Name'] || member['Last Name']) {
5252
const firstName = member?.['First Name']?.trim() || '';
5353
const lastName = member?.['Last Name']?.trim() || '';
@@ -71,17 +71,13 @@ const processCsv = async ({csvPath}: {csvPath: string}) => {
7171
newMember.stripe_customer_id = 'auto';
7272
}
7373

74-
if (member['Free Tier'] === 'Yes') {
75-
newMember.labels.push(`beehiiv-tier-free`);
76-
}
77-
78-
if (member['Premium Tier'] === 'Yes') {
79-
newMember.labels.push(`beehiiv-premium-free`);
80-
}
81-
82-
if (member.tier) {
83-
newMember.labels.push(`beehiiv-tier-${member.tier}`);
84-
}
74+
const tierKeys = Object.keys(member).filter(key => key.endsWith('Tier'));
75+
tierKeys.forEach((key: string) => {
76+
if (member[key] === 'Yes') {
77+
const slugifiedTier = slugify(key);
78+
newMember.labels.push(`beehiiv-tier-${slugifiedTier}`);
79+
}
80+
});
8581

8682
if (member.stripe_customer_id) {
8783
allMembers.paid.push(newMember);
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
email,First Name,Last Name,status,tier,created_at,stripe_customer_id,Free Tier,Premium Tier,unsubscribed_at,Subscription Date (Substack)
2-
lorem@example.com, Lorem Name ,,active,free,2023-03-24 10:23:10 UTC,,Yes,No,,
3-
ipsum@example.com, Ipsum , Name ,active,premium,2023-03-24 10:36:47 UTC,,No,Yes,,
4-
dolor@example.com, , Dolor Name ,inactive,free,2023-03-24 10:36:48 UTC,,Yes,No,,
5-
simet@example.com,,,inactive,premium,2023-03-24 10:37:04 UTC,,No,Yes,,
6-
amett@example.com,,,pending,free,2023-04-11 14:02:46 UTC,,Yes,No,,
7-
elit@example.com,,,active,premium,2023-04-11 14:02:46 UTC,cus_1234,No,Yes,,
8-
nullfirst@example.com,, Last Only ,active,free,2023-04-11 14:02:47 UTC,,Yes,No,,
9-
unsublit@example.com,,,active,premium,2023-04-11 14:02:46 UTC,cus_1234,No,Yes,2024-03-13 11:33:53 UTC,
10-
fromsubstack@example.com,,,active,free,2023-04-11 14:02:46 UTC,,Yes,No,,2022-03-12 11:43:23 UTC
1+
email,First Name,Last Name,status,tier,created_at,stripe_customer_id,Free Tier,Bronze Tier,Silver Tier,Gold Tier,unsubscribed_at,Subscription Date (Substack)
2+
lorem@example.com, Lorem Name ,,active,free,2023-03-24 10:23:10 UTC,,Yes,No,No,No,,
3+
ipsum@example.com, Ipsum , Name ,active,premium,2023-03-24 10:36:47 UTC,,No,No,Yes,No,,
4+
dolor@example.com, , Dolor Name ,inactive,free,2023-03-24 10:36:48 UTC,,Yes,No,No,No,,
5+
simet@example.com,,,inactive,premium,2023-03-24 10:37:04 UTC,,No,No,No,No,,
6+
amett@example.com,,,pending,free,2023-04-11 14:02:46 UTC,,Yes,No,No,No,,
7+
elit@example.com,,,active,premium,2023-04-11 14:02:46 UTC,cus_1234,No,Yes,No,No,,
8+
nullfirst@example.com,, Last Only ,active,free,2023-04-11 14:02:47 UTC,,No,No,No,Yes,,
9+
unsublit@example.com,,,active,premium,2023-04-11 14:02:46 UTC,cus_1234,No,Yes,No,No,2024-03-13 11:33:53 UTC,
10+
fromsubstack@example.com,,,active,free,2023-04-11 14:02:46 UTC,,Yes,No,No,No,,2022-03-12 11:43:23 UTC

packages/mg-beehiiv-members/src/test/process.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@ describe('beehiiv Members', () => {
1919
assert.equal(processed.paid[0].stripe_customer_id, 'cus_1234');
2020
});
2121

22+
it('Sets tiers as labels', async () => {
23+
const processed = await processCsv({csvPath: join(fixturesPath, 'members.csv')});
24+
25+
// Free tier
26+
assert.equal(processed.free[0].email, 'lorem@example.com');
27+
assert.equal(processed.free[0].labels.length, 2);
28+
assert.equal(processed.free[0].labels[0], 'beehiiv-status-active');
29+
assert.equal(processed.free[0].labels[1], 'beehiiv-tier-free-tier');
30+
31+
// Bronze tier
32+
assert.equal(processed.paid[0].email, 'elit@example.com');
33+
assert.equal(processed.paid[0].labels.length, 2);
34+
assert.equal(processed.paid[0].labels[0], 'beehiiv-status-active');
35+
assert.equal(processed.paid[0].labels[1], 'beehiiv-tier-bronze-tier');
36+
37+
// Silver tier
38+
assert.equal(processed.free[1].email, 'ipsum@example.com');
39+
assert.equal(processed.free[1].labels.length, 2);
40+
assert.equal(processed.free[1].labels[0], 'beehiiv-status-active');
41+
assert.equal(processed.free[1].labels[1], 'beehiiv-tier-silver-tier');
42+
43+
// Gold tier
44+
assert.equal(processed.free[3].email, 'nullfirst@example.com');
45+
assert.equal(processed.free[3].labels.length, 2);
46+
assert.equal(processed.free[3].labels[0], 'beehiiv-status-active');
47+
assert.equal(processed.free[3].labels[1], 'beehiiv-tier-gold-tier');
48+
});
49+
2250
it('Uses names is present', async () => {
2351
const processed = await processCsv({csvPath: join(fixturesPath, 'members.csv')});
2452

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
declare module '@tryghost/string';
12
declare module '@tryghost/mg-fs-utils';

0 commit comments

Comments
 (0)