Skip to content

Commit a207b22

Browse files
authored
refactorAndAddBookingOps (#19)
1 parent b6746f0 commit a207b22

22 files changed

+614
-73
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.9",
3+
"version": "0.2.0",
44
"description": "Automatically generated by graphql-editor-cli",
55
"main": "lib/index.js",
66
"scripts": {

packages/integrations/gei-bookings/schema.graphql

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ type UserMutation{
6262
bookService(
6363
input: BookServiceInput!
6464
): BookServiceRespond!
65+
updateBooking(
66+
input: UpdateBookingInput!
67+
): BookServiceRespond!
68+
removeBooking(
69+
_id: String!
70+
): RemoveServiceRespond!
6571
send(
6672
mailgunData: MailgunData!
6773
): String
@@ -70,6 +76,28 @@ type UserMutation{
7076
): RespondOnServiceRequestRespond!
7177
}
7278

79+
input UpdateBookingInput{
80+
_id: String!
81+
addServiceIds: [String!]
82+
removeServiceIds: [String!]
83+
comments: UpdateReservationInfoInput
84+
}
85+
86+
input UserInput{
87+
name: String!
88+
secondName: String
89+
phone: String
90+
email: String
91+
}
92+
93+
input UpdateUserInput{
94+
name: String
95+
bookerId: String
96+
secondName: String
97+
phone: String
98+
email: String
99+
}
100+
73101
input MailgunData{
74102
to: String!
75103
subject: String!
@@ -174,6 +202,49 @@ input RegisterServiceInput{
174202
active: Boolean
175203
}
176204

205+
input UpdateReservationInfoInput{
206+
user: UpdateUserInput
207+
comments: String
208+
numberOfGuests: Int
209+
numberOfKids: Int
210+
animals: Boolean
211+
lateCheckIn: Boolean
212+
from: Date
213+
to: Date
214+
price: Int
215+
}
216+
217+
input ReservationInfoInput{
218+
user: UserInput
219+
comments: String
220+
numberOfGuests: Int
221+
numberOfKids: Int
222+
animals: Boolean
223+
lateCheckIn: Boolean
224+
from: Date
225+
to: Date
226+
price: Int
227+
}
228+
229+
type Comments{
230+
user: User
231+
comments: String
232+
numberOfGuests: Int
233+
numberOfKids: Int
234+
animals: Boolean
235+
lateCheckIn: Boolean
236+
from: Date
237+
to: Date
238+
price: Int
239+
}
240+
241+
type User{
242+
name: String!
243+
secondName: String
244+
phone: String
245+
email: String
246+
}
247+
177248
type RegisterServiceRespond{
178249
service: [Service!]
179250
error: GlobalError
@@ -206,7 +277,7 @@ type RemoveBookingRespond{
206277

207278
input BookServiceInput{
208279
serviceIds: [String!]!
209-
comments: String
280+
comments: ReservationInfoInput
210281
}
211282

212283
type BookServiceRespond{
@@ -239,7 +310,7 @@ type Service implements dbEssentials{
239310
type BookingRecord implements dbEssentials{
240311
bookerId: String!
241312
services: [Service!]
242-
comments: String
313+
comments: Comments
243314
_id: String!
244315
createdAt: Date!
245316
status: BookStatus!
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
33
import { GlobalError, convertDatedObjToString, errMiddleware } from '../utils/middleware.js';
4-
import { orm } from '../utils/db/orm.js';
4+
import { MongoOrb } from '../utils/db/orm.js';
55

66
export const getService = async (input: FieldResolveInput) =>
77
resolverFor('PublicQuery', 'getService', async (args) =>
88
errMiddleware(async () => {
9-
return await orm()
10-
.then(async (o) => ({
11-
service: convertDatedObjToString(await o('Services').collection.findOne({ _id: args.serviceId, active: { $ne: false } })),
12-
}))
13-
.catch((r) => {
14-
throw new GlobalError(r, import.meta.url);
15-
});
9+
return ({
10+
service: convertDatedObjToString(await MongoOrb('Services').collection.findOne({ _id: args.serviceId, active: { $ne: false } }).catch((r) => {
11+
throw new GlobalError(r, import.meta.url);
12+
}))
13+
})
14+
1615
}),
1716
)(input.arguments);
1817
export default getService;

packages/integrations/gei-bookings/src/PublicQuery/listServices.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
33
import { convertDateObjToStringForArray, errMiddleware } from '../utils/middleware.js';
4-
import { orm, preparePageOptions } from '../utils/db/orm.js';
4+
import { MongoOrb, preparePageOptions } from '../utils/db/orm.js';
55
import { ServicesCollection } from '../utils/db/collections.js';
66

77
export const isScalarDate = (obj: unknown): boolean => typeof obj === 'string' && obj !== null && !!Date.parse(obj);
@@ -19,8 +19,8 @@ export const listServices = async (input: FieldResolveInput) =>
1919
const toDate = isScalarDate(args?.input?.filters?.toDate)
2020
? isScalarDate(args?.input?.filters?.toDate)
2121
: undefined;
22-
return await orm().then(async (o) => ({
23-
services: convertDateObjToStringForArray(await o(ServicesCollection)
22+
return {
23+
services: convertDateObjToStringForArray(await MongoOrb(ServicesCollection)
2424
.collection.find({
2525
...pa,
2626
...(fromDate && { startDate: { $gte: new Date(args?.input?.filters?.fromDate as string) } }),
@@ -36,7 +36,7 @@ export const listServices = async (input: FieldResolveInput) =>
3636
.skip(po.skip)
3737
.sort('createdAt', -1)
3838
.toArray(),
39-
)}));
39+
)};
4040
}),
4141
)(input.arguments);
4242
export default listServices;

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { BookStatus, resolverFor } from '../zeus/index.js';
3-
import { orm } from '../utils/db/orm.js';
3+
import { MongoOrb } from '../utils/db/orm.js';
44
import { GlobalError, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
55
import { ObjectId, WithId } from 'mongodb';
66
import { ServiceModel } from '../models/ServiceModel.js';
@@ -9,14 +9,11 @@ export const bookService = async (input: FieldResolveInput) =>
99
resolverFor('UserMutation', 'bookService', async (args, src) =>
1010
errMiddleware(async () => {
1111
sourceContainUserIdOrThrow(src);
12-
const o = await orm();
13-
14-
15-
const bookServices = await Promise.all(
12+
const bookServices = await Promise.all(
1613
args.input.serviceIds.map(
1714
async (serviceId: string) =>
1815
(
19-
await o('Services').collection.findOneAndUpdate(
16+
await MongoOrb('Services').collection.findOneAndUpdate(
2017
{ _id: serviceId, taken: { $ne: true } },
2118
{ $set: { taken: true } },
2219
)
@@ -29,7 +26,7 @@ export const bookService = async (input: FieldResolveInput) =>
2926
];
3027

3128
if (busy[0]) {
32-
await o('Services').collection.updateMany(
29+
await MongoOrb('Services').collection.updateMany(
3330
{ _id: { $in: bookedServices.map((s: any) => s._id) } },
3431
{ $set: { taken: false } },
3532
);
@@ -38,17 +35,36 @@ export const bookService = async (input: FieldResolveInput) =>
3835

3936

4037

41-
const book = await o('Bookings')
38+
const book = await MongoOrb('Bookings')
4239
.collection.insertOne(
4340
{
4441
_id: new ObjectId().toHexString(),
4542
createdAt: new Date(),
46-
bookerId: src.userId,
43+
bookerId: src.userId || src._id || args.input.comments?.user?.email,
4744
services: args.input.serviceIds,
48-
comments: args.input.comments ? args.input.comments : undefined,
45+
comments: args.input.comments
46+
? {
47+
...args.input.comments,
48+
user: args.input.comments.user
49+
? {
50+
...args.input.comments.user,
51+
secondName: args.input.comments.user.secondName || undefined,
52+
phone: args.input.comments.user.phone || undefined,
53+
name: args.input.comments.user.name,
54+
email: args.input.comments.user.email || undefined
55+
}
56+
: undefined,
57+
comments: args.input.comments.comments || undefined,
58+
numberOfGuests: args.input.comments.numberOfGuests || undefined,
59+
numberOfKids: args.input.comments.numberOfKids || undefined,
60+
animals: args.input.comments.animals || undefined,
61+
lateCheckIn: args.input.comments.lateCheckIn || undefined,
62+
price: args.input.comments.price || undefined
63+
}
64+
: undefined,
4965
status: bookedServices[0].neededAccept ? BookStatus.PENDING : BookStatus.ACCEPTED,
5066
})
51-
.then(async (c) => o('Bookings').collection.findOne({ _id: c.insertedId } ));
67+
.then(async (c) => MongoOrb('Bookings').collection.findOne({ _id: c.insertedId } ));
5268
if (!book) {
5369
throw new GlobalError('inserted document is null', import.meta.url);
5470
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
33
import { GlobalError, convertDateObjToStringForArray, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
4-
import { mustFindAny, orm } from '../utils/db/orm.js';
4+
import { mustFindAny, MongoOrb } from '../utils/db/orm.js';
55
import { ServicesCollection } from '../utils/db/collections.js';
66
import { ObjectId } from 'mongodb';
77

@@ -27,9 +27,7 @@ resolverFor('UserMutation', 'registerService', async (args, src) =>
2727
time: args.input.time || undefined,
2828
}
2929
})
30-
const insert = await orm().then((o) =>
31-
o(ServicesCollection).collection.insertMany(services),
32-
);
30+
const insert = await MongoOrb(ServicesCollection).collection.insertMany(services);
3331
const insertedIdsArray = Object.values(insert.insertedIds);
3432
const createdServices = await mustFindAny(ServicesCollection, { _id: {$in: insertedIdsArray} });
3533
return { service: convertDateObjToStringForArray(createdServices) };
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import { FieldResolveInput } from 'stucco-js';
3+
import { resolverFor } from '../zeus/index.js';
4+
import { MongoOrb } from '../utils/db/orm.js';
5+
import { GlobalError, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
6+
7+
export const removeBooking = async (input: FieldResolveInput) =>
8+
resolverFor('UserMutation', 'removeBooking', async (args, src) =>
9+
errMiddleware(async () => {
10+
sourceContainUserIdOrThrow(src);
11+
const booking = await MongoOrb('Bookings').collection.findOne({ _id: args._id });
12+
13+
if (!booking || !booking.services)
14+
throw new GlobalError(`Booking not find for _id: ${args._id}`, import.meta.url);
15+
16+
const removedServices = await MongoOrb('Services').collection.updateMany(
17+
{ _id: { $in: booking.services }, taken: { $ne: false } },
18+
{ $set: { taken: false } },
19+
);
20+
if (removedServices.modifiedCount < 1)
21+
throw new GlobalError(`Service with _id: '${booking.services}' for this booking not find`, import.meta.url);
22+
23+
const removeBook = await MongoOrb('Bookings').collection.deleteOne({ _id: args._id });
24+
if (removeBook.deletedCount < 1) throw new GlobalError(`Booking has't been removed`, import.meta.url);
25+
return { removed: removeBook.deletedCount !== 0 };
26+
}))(input.arguments, input.source);
27+
export default removeBooking;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
3-
import { orm } from '../utils/db/orm.js';
3+
import { MongoOrb } from '../utils/db/orm.js';
44
import { sourceContainUserIdOrThrow } from '../utils/middleware.js';
55

66
export const removeService = async (input: FieldResolveInput) =>
@@ -9,13 +9,10 @@ export const removeService = async (input: FieldResolveInput) =>
99
'removeService',
1010
async (args, src) => (
1111
sourceContainUserIdOrThrow(src),
12-
orm()
13-
.then((o) =>
14-
o('Services').collection.updateOne(
12+
MongoOrb('Services').collection.updateOne(
1513
{ _id: args.serviceId, ownerId: src.userId || src._id, active: { $ne: true }, taken: { $ne: true } },
1614
{ $set: { active: false } },
17-
),
18-
)
15+
)
1916
.then((r) => ({ removed: r.modifiedCount !== 0 }))
2017
),
2118
)(input.arguments, input.source);

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { BookStatus, resolverFor } from '../zeus/index.js';
33
import { GlobalError, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
4-
import { orm } from '../utils/db/orm.js';
4+
import { MongoOrb } from '../utils/db/orm.js';
55

66
export const respondOnServiceRequest = async (input: FieldResolveInput) =>
77
resolverFor('UserMutation', 'respondOnServiceRequest', async (args, src) =>
@@ -10,28 +10,26 @@ export const respondOnServiceRequest = async (input: FieldResolveInput) =>
1010
if (args.input?.answer === BookStatus.PENDING) {
1111
throw new GlobalError('answer cannot be pending', import.meta.url);
1212
}
13-
const o = await orm();
14-
await o('Bookings')
13+
MongoOrb('Bookings')
1514
.collection.find({ _id: { $in: args.input.bookIds }, answeredAt: { $exists: false } }).toArray()
1615
.then(async (books) => {
1716
if (!books || books.length < 1) {
1817
throw new GlobalError(`cannot find anyone books for id: ${ args.input.bookIds }`, import.meta.url);
1918
}
20-
await o('Services')
19+
MongoOrb('Services')
2120
.collection.find({ _id: { $in: books.map((b)=> b.services ).flatMap(innerArray => innerArray) }, ownerId: src.userId || src._id})
2221
.toArray().then((r) => {
2322
if (!r || r.length < 1) {
2423
throw new GlobalError('you can answer only on yours services', import.meta.url);
2524
}
2625
});
2726
});
28-
return await orm().then((o) =>
29-
o('Bookings')
27+
return MongoOrb('Bookings')
3028
.collection.updateMany(
3129
{ _id: { $in: args.input.bookIds} },
3230
{ $set: { answeredAt: new Date(), status: args.input.answer } },
3331
)
34-
.then((r) => ({ status: r.modifiedCount !== 0 })),
32+
.then((r) => ({ status: r.modifiedCount !== 0 }),
3533
);
3634
}),
3735
)(input.arguments, input.source);

0 commit comments

Comments
 (0)