Skip to content

Commit 0f74304

Browse files
authored
Recalc total verse equivalents saved as NaN (#2920)
1 parent 068d727 commit 0f74304

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { node, relation } from 'cypher-query-builder';
2+
import { ID } from '~/common';
3+
import { BaseMigration, Migration } from '~/core/database';
4+
import { ACTIVE } from '~/core/database/query';
5+
import { getTotalVerseEquivalents } from '../../scripture/verse-equivalents';
6+
import { ProductService } from '../product.service';
7+
8+
@Migration('2023-10-27T09:47:07')
9+
export class FixNaNTotalVerseEquivalentsMigration extends BaseMigration {
10+
constructor(private readonly productService: ProductService) {
11+
super();
12+
}
13+
14+
async up() {
15+
const session = this.fakeAdminSession;
16+
const ids = await this.db
17+
.query()
18+
.match([
19+
node('product', 'Product'),
20+
relation('out', '', 'totalVerseEquivalents', ACTIVE),
21+
node('tve', 'Property'),
22+
])
23+
.raw('WHERE tve.hadNaN = true or isNaN(tve.value)')
24+
.return<{ id: ID }>('product.id as id')
25+
.map('id')
26+
.run();
27+
28+
const products = await this.productService.readManyUnsecured(
29+
ids,
30+
this.fakeAdminSession,
31+
);
32+
33+
for (const p of products) {
34+
const correctTotalVerseEquivalent = getTotalVerseEquivalents(
35+
...p.scriptureReferences,
36+
);
37+
38+
if (p.__typename === 'DirectScriptureProduct') {
39+
await this.productService.updateDirect(
40+
{ id: p.id, totalVerseEquivalents: correctTotalVerseEquivalent },
41+
session,
42+
);
43+
}
44+
if (p.__typename === 'DerivativeScriptureProduct') {
45+
await this.productService.updateDerivative(
46+
{ id: p.id, totalVerseEquivalents: correctTotalVerseEquivalent },
47+
session,
48+
);
49+
}
50+
}
51+
}
52+
}

src/components/product/product.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FileModule } from '../file/file.module';
44
import { ScriptureModule } from '../scripture';
55
import { StoryModule } from '../story/story.module';
66
import * as handlers from './handlers';
7+
import { FixNaNTotalVerseEquivalentsMigration } from './migrations/fix-nan-total-verse-equivalents.migration';
78
import { ProducibleResolver } from './producible.resolver';
89
import { ProductExtractor } from './product-extractor.service';
910
import { ProductLoader } from './product.loader';
@@ -25,6 +26,7 @@ import { ProductService } from './product.service';
2526
ProductService,
2627
ProductRepository,
2728
ProductExtractor,
29+
FixNaNTotalVerseEquivalentsMigration,
2830
...Object.values(handlers),
2931
],
3032
exports: [ProductService],

0 commit comments

Comments
 (0)