Skip to content

Commit 545ae3f

Browse files
committed
updateBook
1 parent 2dff463 commit 545ae3f

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

packages/integrations/gei-bookings/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gei-bookings",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Automatically generated by graphql-editor-cli",
55
"main": "lib/index.js",
66
"scripts": {

packages/integrations/gei-bookings/src/UserMutation/bookService.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,43 @@ import { FieldResolveInput } from 'stucco-js';
22
import { BookStatus, resolverFor } from '../zeus/index.js';
33
import { orm } from '../utils/db/orm.js';
44
import { GlobalError, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
5-
import { ObjectId } from 'mongodb';
5+
import { ObjectId, WithId } from 'mongodb';
6+
import { ServiceModel } from '../models/ServiceModel.js';
67

78
export const bookService = async (input: FieldResolveInput) =>
89
resolverFor('UserMutation', 'bookService', async (args, src) =>
910
errMiddleware(async () => {
1011
sourceContainUserIdOrThrow(src);
1112
const o = await orm();
1213

13-
const services = await Promise.all(args.input.serviceIds.map(async (serviceId) => {
14-
15-
const service = await o('Services').collection.findOneAndUpdate(
16-
{ _id: serviceId, taken: { $ne: true } },
17-
{ $set: { taken: true } },
18-
);
19-
if (!service.value) {
20-
throw new GlobalError(`service is already taken: ${serviceId}`, import.meta.url);
21-
}
22-
23-
return service.value
24-
}))
2514

15+
const bookServices = await Promise.all(
16+
args.input.serviceIds.map(
17+
async (serviceId: string) =>
18+
(
19+
await o('Services').collection.findOneAndUpdate(
20+
{ _id: serviceId, taken: { $ne: true } },
21+
{ $set: { taken: true } },
22+
)
23+
).value || serviceId,
24+
),
25+
);
26+
const [bookedServices, busy] = [
27+
bookServices.filter((s): s is WithId<ServiceModel> => typeof s !== 'string'),
28+
bookServices.filter((s): s is string => typeof s === 'string')
29+
];
30+
bookServices.forEach(async (s) => {
31+
if (typeof s === 'string') {
32+
await o('Services').collection.updateMany(
33+
{ _id: { $in: bookedServices.map((s: any) => s._id) } },
34+
{ $set: { taken: false } },
35+
);
36+
throw new GlobalError(`Service is already taken: ${s}`, import.meta.url);
37+
}
38+
return s._id;
39+
});
40+
41+
2642
const book = await o('Bookings')
2743
.collection.insertOne(
2844
{
@@ -31,13 +47,13 @@ export const bookService = async (input: FieldResolveInput) =>
3147
bookerId: src.userId,
3248
services: args.input.serviceIds,
3349
comments: args.input.comments ? args.input.comments : undefined,
34-
status: services[0].neededAccept ? BookStatus.PENDING : BookStatus.ACCEPTED,
50+
status: bookedServices[0].neededAccept ? BookStatus.PENDING : BookStatus.ACCEPTED,
3551
})
3652
.then(async (c) => o('Bookings').collection.findOne({ _id: c.insertedId } ));
3753
if (!book) {
3854
throw new GlobalError('inserted document is null', import.meta.url);
3955
}
40-
return { book: { ...book, services: services }};
56+
return { book: { ...book, services: bookedServices }};
4157
}),
4258
)(input.arguments, input.source);
4359
export default bookService;

0 commit comments

Comments
 (0)