|
1 |
| -import importProcedures from './import'; |
2 |
| -import getProcedureUpdates from '../graphql/queries/getProcedureUpdates'; |
3 |
| -import client from '../graphql/client'; |
4 |
| -import ProcedureModel from '../models/Procedure'; |
| 1 | +import importProcedures from "./import"; |
| 2 | +import getProcedureUpdates from "../graphql/queries/getProcedureUpdates"; |
| 3 | +import createClient from "../graphql/client"; |
| 4 | +import ProcedureModel from "../models/Procedure"; |
| 5 | + |
| 6 | +export default async data => { |
| 7 | + const client = createClient(); |
5 | 8 |
|
6 |
| -export default async (data) => { |
7 | 9 | // Count local Data in groups
|
8 |
| - const groups = await ProcedureModel.aggregate([{ |
9 |
| - // Group by Period & Type |
10 |
| - $group: { |
11 |
| - _id: { period: '$period', type: '$type' }, |
12 |
| - count: { $sum: 1 }, |
| 10 | + const groups = await ProcedureModel.aggregate([ |
| 11 | + { |
| 12 | + // Group by Period & Type |
| 13 | + $group: { |
| 14 | + _id: { period: "$period", type: "$type" }, |
| 15 | + count: { $sum: 1 } |
| 16 | + } |
13 | 17 | },
|
14 |
| - }, |
15 |
| - { |
16 |
| - // Group by Period |
17 |
| - $group: { |
18 |
| - _id: '$_id.period', |
19 |
| - types: { $push: { type: '$_id.type', count: '$count' } }, |
| 18 | + { |
| 19 | + // Group by Period |
| 20 | + $group: { |
| 21 | + _id: "$_id.period", |
| 22 | + types: { $push: { type: "$_id.type", count: "$count" } } |
| 23 | + } |
20 | 24 | },
|
21 |
| - }, |
22 |
| - { |
23 |
| - // Rename _id Field to period |
24 |
| - $project: { _id: 0, period: '$_id', types: 1 }, |
25 |
| - }]); |
| 25 | + { |
| 26 | + // Rename _id Field to period |
| 27 | + $project: { _id: 0, period: "$_id", types: 1 } |
| 28 | + } |
| 29 | + ]); |
26 | 30 |
|
27 | 31 | const update = [];
|
28 |
| - await Promise.all(data.map(async (d) => { |
29 |
| - const period = parseInt(d.period, 10); |
30 |
| - const { type, countBefore, changedIds } = d.types.find(t => t.type === 'Gesetzgebung'); |
31 |
| - const group = groups.find(c => c.period === period); |
32 |
| - const localCount = group ? group.types |
33 |
| - .find(ct => ct.type === type).count : 0; |
34 |
| - // Append Changed IDs |
35 |
| - update.concat(changedIds); |
36 |
| - // Compare Counts Remote & Local |
37 |
| - if (countBefore > localCount) { |
38 |
| - // Find remote Procedure Updates |
39 |
| - const { data: { procedureUpdates } } = await client.query({ |
40 |
| - query: getProcedureUpdates, |
41 |
| - variables: { period, type }, |
42 |
| - }); |
43 |
| - // Find local Procedure Updates |
44 |
| - const localProcedureUpdates = await ProcedureModel |
45 |
| - .find({ period, type }, { procedureId: 1, lastUpdateDate: 1 }); |
46 |
| - // Compare |
47 |
| - procedureUpdates.forEach((pu) => { |
48 |
| - const localData = localProcedureUpdates.find(ld => ld.procedureId === pu.procedureId); |
49 |
| - if (!localData || new Date(localData.lastUpdateDate) < new Date(pu.updatedAt)) { |
50 |
| - update.push(pu.procedureId); |
51 |
| - } |
52 |
| - }); |
53 |
| - } |
54 |
| - })); |
| 32 | + await Promise.all( |
| 33 | + data.map(async d => { |
| 34 | + const period = parseInt(d.period, 10); |
| 35 | + const { type, countBefore, changedIds } = d.types.find( |
| 36 | + t => t.type === "Gesetzgebung" |
| 37 | + ); |
| 38 | + const group = groups.find(c => c.period === period); |
| 39 | + const localCount = group |
| 40 | + ? group.types.find(ct => ct.type === type).count |
| 41 | + : 0; |
| 42 | + // Append Changed IDs |
| 43 | + update.concat(changedIds); |
| 44 | + // Compare Counts Remote & Local |
| 45 | + if (countBefore > localCount) { |
| 46 | + // Find remote Procedure Updates |
| 47 | + const { data: { procedureUpdates } } = await client.query({ |
| 48 | + query: getProcedureUpdates, |
| 49 | + variables: { period, type } |
| 50 | + }); |
| 51 | + // Find local Procedure Updates |
| 52 | + const localProcedureUpdates = await ProcedureModel.find( |
| 53 | + { period, type }, |
| 54 | + { procedureId: 1, lastUpdateDate: 1 } |
| 55 | + ); |
| 56 | + // Compare |
| 57 | + procedureUpdates.forEach(pu => { |
| 58 | + const localData = localProcedureUpdates.find( |
| 59 | + ld => ld.procedureId === pu.procedureId |
| 60 | + ); |
| 61 | + if ( |
| 62 | + !localData || |
| 63 | + new Date(localData.lastUpdateDate) < new Date(pu.updatedAt) |
| 64 | + ) { |
| 65 | + update.push(pu.procedureId); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + }) |
| 70 | + ); |
55 | 71 |
|
56 | 72 | // Splitt in Chunks & Update
|
57 | 73 | const chunkSize = 100;
|
|
0 commit comments