Reputation: 1
Hi I'm having issue with getting logged in user auth data to store creator user id while saving the record, its working fine when I save in db with normal api requests but when adding task to queue it is not able to get user data while saving to db, is there some dependancy I'm missing? or how can I fix it?
Attaching my code
I tried accessing contextService directly but that is also failing, not sure how to access user in entity directly.
Upvotes: 0
Views: 635
Reputation: 1
I resolved this with nestjs-cls document on accessing payload outside context, here's the link for more reference: https://github.com/Papooch/nestjs-cls#usage-outside-of-web-request
And set auth user in queue consumer class and then executed the pending task it picked the loggedin user details.
await this.cls.run(async () => {
this.cls.set('authUser', user);
await this.shipmentDocsService.createInvoice(shipment);
});
Upvotes: 0
Reputation: 70450
Why would the job, which is not tied to the request in any way, have information about who kicked it off if you didn't add that data to the job in the first place?
Those jobs that bull runs are asynchronous and the request does not tie to it at all. Add the user to the job data, like how you set up the shipment
, and you should have the user available
Upvotes: 0