|
| 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 | +} |
0 commit comments