Repository navigation
Expand file tree
/
Copy pathseed.ts
More file actions
751 lines (657 loc) · 20.5 KB
/
Copy pathseed.ts
File metadata and controls
751 lines (657 loc) · 20.5 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import type {
categoriesChildInputs,
categoriesScalars,
map_pinsChildInputs,
newsScalars,
profile_badges_relationsScalars,
profile_badgesScalars,
profile_typesScalars,
profilesInputs,
profilesScalars,
project_stepsChildInputs,
projectsScalars,
questionsChildInputs,
questionsScalars,
researchScalars,
stripe_tier_configScalars,
subscribersChildInputs,
subscribersScalars,
tagsChildInputs,
tagsScalars,
useful_votesChildInputs,
useful_votesScalars,
} from '@snaplet/seed';
import { createSeedClient } from '@snaplet/seed';
import { Client } from 'pg';
import libraryJson from './.snaplet/library.json';
import questionsJson from './.snaplet/questions.json';
import { profilesSeed } from './seed/profilesSeed';
import { usersSeed } from './seed/usersSeed';
// Copied from the helper to decouple the seed script from any app builing processes
export const convertToSlug = (text: string) => {
const specialCharactersPattern = /[^a-zA-Z0-9_-]/gi;
const stripSpecialCharacters = (text: string) => {
return text ? text.split(' ').join('-').replace(specialCharactersPattern, '') : '';
};
return stripSpecialCharacters(text).toLowerCase();
};
const tenant_id = `precious-plastic`;
const _QUESTIONS_BASE: Partial<questionsScalars> = {
tenant_id,
moderation: 'accepted',
legacy_id: null,
previous_slugs: [],
images: [],
deleted: false,
tags: [],
};
const _PROJECT_BASE: Partial<projectsScalars> = {
tenant_id,
moderation: 'accepted',
legacy_id: null,
previous_slugs: [],
deleted: false,
tags: [],
is_draft: false,
file_download_count: 0,
total_views: 0,
comment_count: 0,
};
const _PROJECT_STEP_BASE = {
tenant_id,
images: null,
video_url: null,
};
const _CATEGORIES_BASE: Partial<categoriesScalars> = {
tenant_id,
legacy_id: null,
};
const _TAGS_BASE: Partial<tagsScalars> = {
tenant_id,
legacy_id: null,
};
const _SUBSCRIBERS_BASE: Partial<subscribersScalars> = {
tenant_id,
content_type: 'questions',
};
const _USEFUL_VOTES_BASE: Partial<useful_votesScalars> = {
tenant_id,
content_type: 'questions',
};
const _BADGES_BASE: Partial<profile_badgesScalars> = {
tenant_id,
grants_badge_id: null,
available_to: null,
action_label: null,
};
const _TYPES_BASE: Partial<profile_typesScalars> = {
tenant_id,
};
const _BADGES_RELATIONS_BASE: Partial<profile_badges_relationsScalars> = {
tenant_id,
};
const seedTags = (): tagsChildInputs => [{ ..._TAGS_BASE, name: 'tag 1' }];
const seedProfileTypes = (): Partial<profile_typesScalars>[] => [
{
..._TYPES_BASE,
name: 'member',
display_name: 'Member',
is_space: false,
description: '',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-member.svg',
map_pin_name: 'Want to get started',
order: 1,
small_image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/map-member.svg',
},
{
..._TYPES_BASE,
name: 'workspace',
display_name: 'Workspace',
is_space: true,
description: '',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-workspace.svg',
map_pin_name: 'Workspace',
order: 2,
small_image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-workspace-small.svg',
},
{
..._TYPES_BASE,
name: 'machine-builder',
display_name: 'Machine Builder',
is_space: true,
description: '',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-machine.svg',
map_pin_name: 'Machine Builder',
order: 3,
small_image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-machine-small.svg',
},
{
..._TYPES_BASE,
name: 'community-point',
display_name: 'Community Point',
is_space: true,
description: '',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-community.svg',
map_pin_name: 'Community Point',
order: 4,
small_image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-community-small.svg',
},
{
..._TYPES_BASE,
name: 'collection-point',
display_name: 'Collection Point',
is_space: true,
description: '',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-collection.svg',
map_pin_name: 'Collection Point',
order: 5,
small_image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/profile-types/pp-collection-small.svg',
},
];
const seedMemberBadge = (): Partial<profile_badgesScalars> => ({
..._BADGES_BASE,
name: 'member',
display_name: 'Member',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/icons/supporter.svg',
action_url: '/support',
premium_tier: null,
is_audience: true,
available_to: 'non_space',
action_label: 'Become a member',
});
const seedBadges = (memberBadgeId: number): Partial<profile_badgesScalars>[] => [
{
..._BADGES_BASE,
name: 'stripe-tier-1',
display_name: 'Start',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/icons/1%20star.svg',
premium_tier: 1,
is_audience: false,
grants_badge_id: memberBadgeId,
},
{
..._BADGES_BASE,
name: 'stripe-tier-2',
display_name: 'Power',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/icons/2%20stars.svg',
premium_tier: 2,
is_audience: false,
grants_badge_id: memberBadgeId,
},
{
..._BADGES_BASE,
name: 'stripe-tier-3',
display_name: 'Boost',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/icons/3%20stars.svg',
premium_tier: 3,
is_audience: false,
grants_badge_id: memberBadgeId,
},
{
..._BADGES_BASE,
name: 'pro',
display_name: 'PRO',
image_url:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/one-army/icons/pro.svg',
action_url: 'https://www.preciousplastic.com/pro-membership',
premium_tier: 1,
is_audience: true,
grants_badge_id: memberBadgeId,
available_to: 'space',
action_label: 'Go PRO',
},
];
const TIER_COLORS: Record<number, string> = {
1: '#BFDEBA',
2: '#77BDE3',
3: '#FEE77B',
};
const seedStripeTierConfig = (
badges: profile_badgesScalars[],
): Partial<stripe_tier_configScalars>[] => {
const tierBadges = badges.filter((b) => b.name?.startsWith('stripe-tier-'));
return tierBadges.map((badge) => ({
tenant_id,
badge_id: badge.id,
description: 'You help us develop new features, get videos in 4K without ads!',
color: TIER_COLORS[badge.premium_tier ?? 0] ?? '#BFDEBA',
thank_you_image_url: null,
}));
};
/// populates badges: 2/3 of profiles to have: 1 and 2 badges, others remain with no badge
const seedBadgesRelations = (
profiles: profilesScalars[],
badges: profile_badgesScalars[],
): Partial<profile_badges_relationsScalars>[] => {
const relations: Partial<profile_badges_relationsScalars>[] = [];
const profilesPerGroup = Math.ceil(profiles.length / 3);
const oneBadgeGroup = profiles.slice(profilesPerGroup, profilesPerGroup * 2);
const twoBadgesGroup = profiles.slice(profilesPerGroup * 2);
// 1 badge each
for (const profile of oneBadgeGroup) {
relations.push({
..._BADGES_RELATIONS_BASE,
profile_id: profile.id,
profile_badge_id: badges[0].id,
});
}
// all (2) badges each
for (const profile of twoBadgesGroup) {
for (let i = 0; i < badges.length; i++) {
relations.push({
..._BADGES_RELATIONS_BASE,
profile_id: profile.id,
profile_badge_id: badges[i].id,
});
}
}
return relations;
};
const seedCategories = (): categoriesChildInputs => [
{ ..._CATEGORIES_BASE, name: 'Questions', type: 'questions' },
{ ..._CATEGORIES_BASE, name: 'Research', type: 'research' },
{ ..._CATEGORIES_BASE, name: 'Guides', type: 'projects' },
{ ..._CATEGORIES_BASE, name: 'Machines', type: 'projects' },
{ ..._CATEGORIES_BASE, name: 'Moulds', type: 'projects' },
{ ..._CATEGORIES_BASE, name: 'Products', type: 'projects' },
{ ..._CATEGORIES_BASE, name: 'Important Updates', type: 'news' },
];
const seedQuestions = (profile: profilesInputs): questionsChildInputs =>
questionsJson[profile.username!.toString()]?.map((q) => ({
..._QUESTIONS_BASE,
slug: convertToSlug(q.title),
title: q.title,
description: q.description,
comment_count: q.comments?.length || 0,
})) || [];
const seedSubscribers = (questions: questionsScalars[]): subscribersChildInputs =>
questions.map((question) => ({
..._SUBSCRIBERS_BASE,
content_id: question.id,
}));
const seedUsefulVotes = (questions: questionsScalars[]): useful_votesChildInputs =>
questions.map((question) => ({
..._USEFUL_VOTES_BASE,
content_id: question.id,
}));
const bigBlockOfMarkdown = `# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading
## Horizontal Rule
***
## Typographic replacements
Enable typographer option to see result.
(c) (C) (r) (R) (tm) (TM) (p) (P) +-
test.. test... test..... test?..... test!....
!!!!!! ???? ,, -- ---
"Smartypants, double quotes" and 'single quotes'

## Emphasis
**This is bold text**
**This is bold text**
*This is italic text*
*This is italic text*
~~Strikethrough~~
## Blockquotes
> Blockquotes can also be nested...> ...by using additional greater-than signs right next to each other...> ...or with spaces between arrows.
## Lists
Unordered
* Sub-lists are made by indenting 2 spaces:
* Marker character change forces new list start:
* Ac tristique libero volutpat at
- Facilisis in pretium nisl aliquet
* Nulla volutpat aliquam velit
* Very easy!
Ordered
1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit
3. Integer molestie lorem at massa
4. You can use sequential numbers...
Start numbering with offset:
1. foo
2. bar
| ddd | ddd | 333 |
| ---- | --- | --- |
| rthd | dfb | rr |
| fbnv | gf | r |
## Links
[link text](http://dev.nodeca.com)
[link with title](http://nodeca.github.io/pica/demo/ "title text!")
Autoconverted link [https://github.com/nodeca/pica](https://github.com/nodeca/pica) (enable linkify to see)
`;
const base_news: Partial<newsScalars> = {
comment_count: 0,
hero_image: null,
moderation: null,
previous_slugs: [],
tenant_id,
total_views: 0,
};
const seedNews: Partial<newsScalars>[] = [
{
...base_news,
title: 'First news article!',
body: bigBlockOfMarkdown,
slug: 'first-news-article',
summary: 'The first',
},
];
const seedLibrary = (): Partial<projectsScalars>[] => [
...libraryJson.map((p: any) => ({
..._PROJECT_BASE,
slug: convertToSlug(p.title),
title: p.title,
description: p.description,
difficulty_level: p.difficulty_level,
time: p.time,
moderation: p.moderation,
file_link: p.file_link,
comment_count: p.comments?.length || 0,
})),
];
const seedProjectSteps = (projects: projectsScalars[]): project_stepsChildInputs => {
const steps: any[] = [];
libraryJson.forEach((p: any, index: number) => {
if (p.steps && Array.isArray(p.steps)) {
p.steps.forEach((step: any) => {
steps.push({
..._PROJECT_STEP_BASE,
project_id: projects[index]?.id,
title: step.title,
description: step.description,
images: step.images || null,
video_url: step.video_url || null,
});
});
}
});
return steps;
};
const baseResearch: Partial<researchScalars> = {
status: 'in-progress',
previous_slugs: [],
tenant_id,
total_views: 0,
collaborators: [],
};
const mapPins = [
{
lat: 38.7223,
lng: -9.1393,
administrative: 'Lisbon',
country: 'Portugal',
country_code: 'pt',
moderation: 'accepted',
name: 'Lisbon',
post_code: '1700',
},
{
lat: 40.7128,
lng: -74.006,
administrative: 'New York',
country: 'United States',
country_code: 'us',
moderation: 'accepted',
name: 'New York City',
post_code: '10001',
},
{
lat: 51.5074,
lng: -0.1278,
administrative: 'London',
country: 'United Kingdom',
country_code: 'gb',
moderation: 'accepted',
name: 'London',
post_code: 'SW1A',
},
{
lat: 48.8566,
lng: 2.3522,
administrative: 'Paris',
country: 'France',
country_code: 'fr',
moderation: 'accepted',
name: 'Paris',
post_code: '75001',
},
{
lat: 35.6762,
lng: 139.6503,
administrative: 'Tokyo',
country: 'Japan',
country_code: 'jp',
moderation: 'accepted',
name: 'Tokyo',
post_code: '100-0001',
},
{
lat: -33.8688,
lng: 151.2093,
administrative: 'Sydney',
country: 'Australia',
country_code: 'au',
moderation: 'accepted',
name: 'Sydney',
post_code: '2000',
},
{
lat: 52.52,
lng: 13.405,
administrative: 'Berlin',
country: 'Germany',
country_code: 'de',
moderation: 'accepted',
name: 'Berlin',
post_code: '10115',
},
{
lat: 19.4326,
lng: -99.1332,
administrative: 'Mexico City',
country: 'Mexico',
country_code: 'mx',
moderation: 'accepted',
name: 'Mexico City',
post_code: '06000',
},
{
lat: -22.9068,
lng: -43.1729,
administrative: 'Rio de Janeiro',
country: 'Brazil',
country_code: 'br',
moderation: 'accepted',
name: 'Rio de Janeiro',
post_code: '20040',
},
];
const seedMapPins = (profiles: profilesScalars[]): map_pinsChildInputs =>
mapPins.slice(0, profiles.length).map((pin, index) => ({
tenant_id,
profile_id: profiles[index].id,
lat: String(pin.lat),
lng: String(pin.lng),
country: pin.country!,
country_code: pin.country_code!,
moderation: pin.moderation!,
administrative: pin.administrative,
post_code: pin.post_code,
moderation_feedback: (pin as any).moderation_feedback,
name: pin.name,
}));
const seedResearch: Partial<researchScalars>[] = [
{
...baseResearch,
title: 'The First Big Old Research Topic',
description: 'This is a super important area to investigate.',
slug: 'the-first-big-old-research-topic',
},
];
const seedBuckets = async () => {
const client = new Client({
connectionString: 'postgresql://postgres:postgres@localhost:54322/postgres',
});
await client.connect();
await client.query(
`insert into storage.buckets (id, name, public, allowed_mime_types)
values ($1, $1, true, '{}'), ($2, $2, false, '{}')
on conflict (id) do update
set name = excluded.name,
public = excluded.public,
allowed_mime_types = excluded.allowed_mime_types`,
[tenant_id, `${tenant_id}-documents`],
);
await client.end();
};
const main = async () => {
const seed = await createSeedClient();
await seed.$resetDatabase();
await seedBuckets();
await seed.tenant_settings([
{
site_name: 'Local Development Community',
site_name_short: 'Local Dev',
site_description:
'A series of tools to collaborate around the world. Connect, share and meet each other to tackle problems.',
site_url: 'http://localhost:3000',
message_sign_off: 'The Dev Team',
email_from: 'platform@onearmy.earth',
site_image:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/precious-plastic/pp-logo.png',
site_favicon:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/precious-plastic/pp-logo.png',
academy_resource: 'https://onearmy.github.io/academy/',
no_messaging: false,
library_heading: 'The largest open source library of plastic recycling tools',
profile_guidelines: 'https://community.preciousplastic.com/academy/guides/platform',
questions_guidelines:
'https://community.preciousplastic.com/academy/guides/guidelines-questions',
supported_modules: 'library,map,research,academy,questions,news',
hidden_modules: null,
organisation_signup_description:
'Are you working with small-scale plastic recycling? Do you wish to become part of <a href="/academy">our universe</a>? Then apply as an organisation here. It usually takes a day or two to get approved.',
organisation_activity: 'small-scale recycling',
donation_settings: {
defaultDescription:
'All of the content here is free. Your donation supports this library of open source recycling knowledge. Making it possible for everyone in the world to use it and start recycling.',
defaultCampaignId: 'ppcpdonor',
spaceDescription:
'Support this space so they can continue to help recycling plastic and share more Open Source information. Your support fuels the small scale network of plastic recyclers.',
defaultImageUrl: '/assets/img/precious-plastic/donation-banner.jpg',
},
tenant_id,
color_primary: '#fee77b',
color_primary_hover: '#ffde45',
color_accent: '#fee77b',
color_accent_hover: '#ffde45',
color_secondary: '#fff0b4',
news_cta_title: 'Just for members',
news_cta_body: 'Become a member to read every article and support the project.',
news_cta_image:
'https://wbskztclbriekwpehznv.supabase.co/storage/v1/object/public/precious-plastic/pp-logo.png',
show_impact: true,
create_research_roles: ['admin', 'research_creator'],
ga_tracking_id: 'test_ga_id',
pwa_icons: {
'16': '',
'32': '',
'192': '',
'256': '',
'512': '',
},
},
]);
const { users } = await seed.users(usersSeed());
// await seed.profile_tags(seedProfileTags())
const { profile_types } = await seed.profile_types(seedProfileTypes());
const { profiles } = await seed.profiles(
(profilesSeed(tenant_id) as any[]).map((profile: profilesInputs, index) => ({
...profile,
moderation: null,
moderation_feedback: null,
auth_id: users[index].id,
profile_type: (
profile_types.find(
(t) =>
t.name ===
(['member', 'workspace', 'machine-builder', 'community-point', 'collection-point'][
index
] ?? 'member'),
) ?? profile_types[0]
).id,
})),
);
const {
profile_badges: [memberBadge],
} = await seed.profile_badges([seedMemberBadge()]);
const { profile_badges } = await seed.profile_badges(seedBadges(memberBadge.id));
await seed.profile_badges_relations(seedBadgesRelations(profiles, profile_badges));
await seed.stripe_tier_config(seedStripeTierConfig(profile_badges));
await seed.map_pins(seedMapPins(profiles));
const { tags } = await seed.tags(seedTags());
const { categories } = await seed.categories(seedCategories());
const questionsAcc: questionsScalars[] = [];
for (const profile of profiles) {
const { questions } = await seed.questions(seedQuestions(profile), {
connect: { profiles: [profile] },
});
questionsAcc.push(...questions);
}
for (const profile of profiles) {
const questionsExceptMine = questionsAcc.filter((q) => q.created_by !== profile.id);
// await seed.comments(seedComments(profile, questionsExceptMine), {
// connect: { profiles: [profile] },
// })
await seed.subscribers(seedSubscribers(questionsExceptMine), {
connect: { profiles: [profile] },
});
await seed.useful_votes(seedUsefulVotes(questionsExceptMine), {
connect: { profiles: [profile] },
});
}
await seed.news(
seedNews.map((item) => ({
...item,
category: categories.find((cat) => cat.type === 'news')?.id,
created_by: profiles[0].id,
tags: [tags[0].id],
})),
);
const { projects } = await seed.projects(
seedLibrary().map((item) => ({
...item,
category: categories.find((cat) => cat.type === 'projects')?.id,
created_by: profiles[0].id,
tags: [tags[0].name],
})),
);
await seed.project_steps(seedProjectSteps(projects));
await seed.research(
seedResearch.map((item) => ({
...item,
category: categories.find((cat) => cat.type === 'research')?.id,
created_by: profiles[0].id,
tags: [tags[0].id.toString()],
})),
);
process.exit();
};
main();