43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { BBWatcher, WBWatcher, Watcher } from 'oak-domain/lib/types/Watcher';
|
||
import { EntityDict } from '../oak-app-domain';
|
||
import { BRC } from '../types/RuntimeCxt';
|
||
import { OperationResult } from 'oak-domain/lib/types';
|
||
import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
|
||
import { shipProjection, refreshtShipState } from '../utils/ship';
|
||
|
||
const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 600 * 1000 : 60 * 1000;
|
||
|
||
const watchers: Watcher<EntityDict, 'ship', BRC>[] = [
|
||
{
|
||
name: '对shipping状态的物流,同步其真实状态',
|
||
entity: 'ship',
|
||
filter: async () => {
|
||
const now = Date.now();
|
||
return {
|
||
iState: 'shipping',
|
||
$$updateAt$$: {
|
||
$lte: now - QUERY_PAYING_STATE_GAP,
|
||
},
|
||
};
|
||
},
|
||
projection: shipProjection,
|
||
fn: async (context, data) => {
|
||
const results = [] as OperationResult<EntityDict>[];
|
||
for (const ship of data) {
|
||
const result = await refreshtShipState(ship as EntityDict['ship']['Schema'], context);
|
||
if (result) {
|
||
results.push(result);
|
||
}
|
||
}
|
||
|
||
if (results.length === 0) {
|
||
return {};
|
||
}
|
||
return results.reduce(
|
||
(prev, cur) => mergeOperationResult(prev, cur)
|
||
);
|
||
}
|
||
}
|
||
];
|
||
|
||
export default watchers; |