Skip to content

Commit cf8751a

Browse files
authored
[Samples] remove ! assertion in communication-job-router sample (#36738)
Resolves #36733
1 parent 00a76a5 commit cf8751a

File tree

1 file changed

+11
-9
lines changed
  • sdk/communication/communication-job-router-rest/samples-dev

1 file changed

+11
-9
lines changed

sdk/communication/communication-job-router-rest/samples-dev/QuickStart.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* @summary Quick start workflow for creating queue, job and worker, routing/matching job with worker
66
*/
77
import type {
8-
RouterWorkerOutput,
98
AzureCommunicationRoutingServiceClient,
109
AcceptJobOfferResultOutput,
1110
RouterJobOutput,
@@ -98,16 +97,20 @@ async function quickStart(): Promise<void> {
9897
if (isUnexpected(workerResponse)) {
9998
throw workerResponse;
10099
}
101-
const workerResult = workerResponse.body as RouterWorkerOutput;
102100

103-
for (const offer of workerResult.offers!) {
101+
const offers = workerResponse.body.offers;
102+
if (!offers || offers.length === 0) {
103+
throw new Error(`No offers found for worker ${workerId}`);
104+
}
105+
106+
for (const offer of offers) {
104107
console.log(`Worker ${workerId} has an active offer for job ${offer.jobId}`);
105108
}
106109

107110
// Accepting an offer
108111
// Once a worker receives an offer, it can take two possible actions: accept or decline. We are going to accept the offer.
109112
// fetching the offer id
110-
const jobOffer = workerResult.offers![0];
113+
const jobOffer = offers[0];
111114

112115
const offerId = jobOffer.offerId; // `OfferId` can be retrieved directly from consuming event from Event grid
113116

@@ -130,12 +133,11 @@ async function quickStart(): Promise<void> {
130133
if (isUnexpected(updatedJobResponse)) {
131134
throw updatedJobResponse;
132135
}
133-
let updatedJob = updatedJobResponse.body as RouterJobOutput;
134136

135-
console.log(`Job assignment has been successful:
136-
${updatedJob.status === "assigned" &&
137-
Object.prototype.hasOwnProperty.call(updatedJob.assignments!, acceptJobOfferResult.assignmentId)
138-
}`);
137+
let updatedJob = updatedJobResponse.body;
138+
const jobResult = updatedJob.status === "assigned" && updatedJob.assignments &&
139+
acceptJobOfferResult.assignmentId in updatedJob.assignments;
140+
console.log(`Job assignment has been successful: ${jobResult}`);
139141

140142
// Completing a job
141143
// Once the worker is done with the job, the worker has to mark the job as `completed`.

0 commit comments

Comments
 (0)