Skip to content

Commit 5d46b5d

Browse files
willdchCarsonF
andauthored
0701 default marketing region migration (#3044)
Co-authored-by: Carson Full <[email protected]>
1 parent 016eb37 commit 5d46b5d

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/components/location/location.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LocationLoader } from './location.loader';
77
import { LocationRepository } from './location.repository';
88
import { LocationResolver } from './location.resolver';
99
import { LocationService } from './location.service';
10+
import { DefaultMarketingRegionMigration } from './migrations/default-marketing-region.migration';
1011

1112
@Module({
1213
imports: [
@@ -20,6 +21,7 @@ import { LocationService } from './location.service';
2021
LocationService,
2122
LocationRepository,
2223
LocationLoader,
24+
DefaultMarketingRegionMigration,
2325
],
2426
exports: [LocationService],
2527
})
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { mapEntries } from '@seedcompany/common';
2+
import { node, relation } from 'cypher-query-builder';
3+
import { ValueOf } from 'type-fest';
4+
import { ID, IdOf } from '~/common';
5+
import { BaseMigration, Migration } from '~/core/database';
6+
import { ACTIVE } from '~/core/database/query';
7+
import { Location } from '../dto';
8+
import { LocationService } from '../location.service';
9+
10+
@Migration('2024-01-16T11:00:00')
11+
export class DefaultMarketingRegionMigration extends BaseMigration {
12+
constructor(private readonly locationService: LocationService) {
13+
super();
14+
}
15+
async up() {
16+
const session = this.fakeAdminSession;
17+
18+
const fieldRegionNameToMarketingRegionName = {
19+
'Africa - Anglophone East': 'Africa',
20+
'Africa - Anglophone West': 'Africa',
21+
'Africa - Congo Basin': 'Africa',
22+
'Africa - Sahel': 'Africa',
23+
'Africa - Southern': 'Africa',
24+
Americas: 'Americas',
25+
Eurasia: 'Europe and the Middle East',
26+
Pacific: 'Pacific',
27+
'Asia - Islands': 'Pacific',
28+
'Asia - Mainland': 'Asia',
29+
'Asia - South': 'Asia',
30+
} as const;
31+
32+
const marketingRegionList = await this.db
33+
.query()
34+
.match([
35+
node('location', 'Location'),
36+
relation('out', '', 'type', ACTIVE),
37+
node('', 'LocationType', { value: 'Region' }),
38+
])
39+
.match([
40+
node('location'),
41+
relation('out', '', 'name', ACTIVE),
42+
node('locName', 'LocationName'),
43+
])
44+
.return<{
45+
id: IdOf<Location>;
46+
name: ValueOf<typeof fieldRegionNameToMarketingRegionName>;
47+
}>(['location.id as id', 'locName.value as name'])
48+
.run();
49+
const marketingRegionNameToId = mapEntries(marketingRegionList, (loc) => [
50+
loc.name,
51+
loc.id,
52+
]).asRecord;
53+
54+
const countries = await this.db
55+
.query()
56+
.match([
57+
node('location', 'Location'),
58+
relation('out', '', 'type', ACTIVE),
59+
node('', 'LocationType', { value: 'Country' }),
60+
])
61+
.match([
62+
node('location', 'Location'),
63+
relation('out', '', 'defaultFieldRegion', ACTIVE),
64+
node('fieldRegion', 'FieldRegion'),
65+
relation('out', '', 'name', ACTIVE),
66+
node('fieldRegionName', 'FieldRegionName'),
67+
])
68+
.return<{
69+
id: ID;
70+
fieldRegionName: keyof typeof fieldRegionNameToMarketingRegionName;
71+
}>(['location.id as id', 'fieldRegionName.value as fieldRegionName'])
72+
.run();
73+
74+
for (const country of countries) {
75+
const marketingRegionName =
76+
fieldRegionNameToMarketingRegionName[country.fieldRegionName];
77+
const marketingRegionId = marketingRegionNameToId[marketingRegionName];
78+
if (marketingRegionId === undefined) {
79+
continue;
80+
}
81+
82+
await this.locationService.update(
83+
{
84+
id: country.id,
85+
defaultMarketingRegionId: marketingRegionId,
86+
},
87+
session,
88+
);
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)