Skip to content

Commit 4f61406

Browse files
committed
Fixed toHexString is not a function
For some unknown reason, the data for a job when it is run no longer contains the user _id as an ObjectId, even though when the job was added to the queue, it was an ObjectId. Instead it emerges as a string. To fix, just convert back to ObjectId before we do anything else with the job. Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>
1 parent db46972 commit 4f61406

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/tasks/delete-for-domain/delete-for-domain.processor.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Forwarder } from 'src/forwarders/class/forwarder.class'
99
import { ForwardersService } from 'src/forwarders/forwarders.service'
1010

1111
import { DeleteForDomainData } from './delete-for-domain.interfaces'
12+
import { ObjectId } from 'mongodb'
1213

1314
@Processor('deleteForDomain')
1415
export class DeleteForDomainProcessor {
@@ -22,6 +23,9 @@ export class DeleteForDomainProcessor {
2223

2324
@Process({ name: 'deleteAccounts' })
2425
private async processDeleteAccounts(job: Job<DeleteForDomainData>): Promise<void> {
26+
// Job data contains _id as string, not as ObjectId for unknown reason
27+
job.data.user._id = new ObjectId(job.data.user._id)
28+
2529
let accounts: AccountListItem[] = []
2630
try {
2731
accounts = await this.accountsService.getAccounts(job.data.user, job.data.domain)
@@ -55,6 +59,9 @@ export class DeleteForDomainProcessor {
5559

5660
@Process({ name: 'deleteForwarders' })
5761
private async processDeleteForwarders(job: Job<DeleteForDomainData>): Promise<void> {
62+
// Job data contains _id as string, not as ObjectId for unknown reason
63+
job.data.user._id = new ObjectId(job.data.user._id)
64+
5865
let forwarders: Forwarder[] = []
5966
try {
6067
forwarders = await this.forwardersService.getForwarders(job.data.user, job.data.domain)
@@ -88,6 +95,9 @@ export class DeleteForDomainProcessor {
8895

8996
@Process({ name: 'deleteAccountAliases' })
9097
private async processDeleteAccountAliases(job: Job<DeleteForDomainData>): Promise<void> {
98+
// Job data contains _id as string, not as ObjectId for unknown reason
99+
job.data.user._id = new ObjectId(job.data.user._id)
100+
91101
const accountAliases = await this.accountsService.getAliases(job.data.user, job.data.domain)
92102
if (!accountAliases || accountAliases.length === 0) {
93103
return
@@ -114,6 +124,9 @@ export class DeleteForDomainProcessor {
114124

115125
@Process({ name: 'deleteAliases' })
116126
private async processDeleteAliases(job: Job<DeleteForDomainData>): Promise<void> {
127+
// Job data contains _id as string, not as ObjectId for unknown reason
128+
job.data.user._id = new ObjectId(job.data.user._id)
129+
117130
const aliases = job.data.user.domains.find((domain) => domain.domain === job.data.domain).aliases
118131
if (!aliases || aliases.length === 0) {
119132
return
@@ -141,7 +154,9 @@ export class DeleteForDomainProcessor {
141154
@OnQueueActive()
142155
private onActive(job: Job<DeleteForDomainData>): void {
143156
this.logger.log(
144-
`Processing job ${job.id} (${job.name}) for user ${job.data.user._id} and domain ${job.data.domain}`,
157+
`Processing job ${job.id} (${job.name}) for user ${job.data.user._id.toHexString()} and domain ${
158+
job.data.domain
159+
}`,
145160
)
146161
}
147162

0 commit comments

Comments
 (0)