1
1
import io from '@pm2/io'
2
2
import { MetricType } from '@pm2/io/build/main/services/metrics'
3
+ import { ServerScope } from 'nano'
3
4
5
+ import { devicesSetup } from '../db/couchDevices'
4
6
import { syncedSettings } from '../db/couchSettings'
7
+ import { devicesByCurrencyHours } from '../db/couchUsers'
5
8
import { CurrencyThreshold } from '../models/CurrencyThreshold'
6
- import { Device } from '../models/Device'
7
- import { User } from '../models/User'
9
+ import { Device } from '../types/pushTypes'
8
10
import { PushResult , PushSender } from '../util/pushSender'
9
11
import { fetchThresholdPrice } from './fetchThresholdPrices'
10
12
@@ -23,7 +25,10 @@ export interface NotificationPriceChange {
23
25
priceChange : number
24
26
}
25
27
26
- export async function checkPriceChanges ( sender : PushSender ) : Promise < void > {
28
+ export async function checkPriceChanges (
29
+ connection : ServerScope ,
30
+ sender : PushSender
31
+ ) : Promise < void > {
27
32
// Sends a notification to devices about a price change
28
33
async function sendNotification (
29
34
thresholdPrice : NotificationPriceChange ,
@@ -78,7 +83,7 @@ export async function checkPriceChanges(sender: PushSender): Promise<void> {
78
83
deviceIds . push ( deviceId )
79
84
}
80
85
}
81
- const tokenGenerator = deviceTokenGenerator ( deviceIds )
86
+ const tokenGenerator = deviceTokenGenerator ( connection , deviceIds )
82
87
let done = false
83
88
let successCount = 0
84
89
let failureCount = 0
@@ -126,14 +131,16 @@ export async function checkPriceChanges(sender: PushSender): Promise<void> {
126
131
}
127
132
128
133
async function * deviceTokenGenerator (
134
+ connection : ServerScope ,
129
135
deviceIds : string [ ]
130
136
) : AsyncGenerator < string [ ] , string [ ] > {
131
137
const tokenSet : Set < string > = new Set ( )
132
138
let tokens : string [ ] = [ ]
133
139
let bookmark : string | undefined
134
140
let done = false
141
+ const dbDevices = connection . use < Device > ( devicesSetup . name )
135
142
while ( ! done ) {
136
- const response = await Device . table . find ( {
143
+ const response = await dbDevices . find ( {
137
144
bookmark,
138
145
selector : {
139
146
_id : {
0 commit comments