Skip to content

Commit 4b16d60

Browse files
committed
fixes
1 parent 86d525a commit 4b16d60

16 files changed

+173
-118
lines changed

package-lock.json

+28-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/integrations/gei-bookings/package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"start": "gecli dev",
88
"build": "tsc",
99
"watch": "tsc --watch",
10+
"lint:fix": "npm run lint -ws --if-present -- --fix",
1011
"update": "gecli codegen models && gecli schema pull && gecli codegen typings",
1112
"integrate": "gecli gei integrate",
1213
"publish": "gecli gei publish",
@@ -24,6 +25,10 @@
2425
"i-graphql": "^0.1.2",
2526
"mongodb": "^5.1.0",
2627
"node-fetch": "^3.3.0",
27-
"stucco-js": "^0.10.18"
28+
"stucco-js": "^0.10.18",
29+
"ws": "^8.16.0"
30+
},
31+
"devDependencies": {
32+
"@types/ws": "^8.5.10"
2833
}
2934
}

packages/integrations/gei-bookings/schema.graphql

+8-9
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ type UserMutation{
5555
): RegisterServiceRespond
5656
updateService(
5757
input: [UpdateServiceInput!]!
58-
serviceId: String!
5958
): UpdateServiceRespond!
6059
removeService(
6160
serviceId: String!
6261
): RemoveServiceRespond!
6362
bookService(
6463
input: BookServiceInput!
65-
serviceId: String!
6664
): BookServiceRespond!
6765
respondOnServiceRequest(
6866
input: RespondOnServiceRequestInput!
@@ -87,7 +85,7 @@ type GetBookingsForServiceRespond{
8785
}
8886

8987
input RespondOnServiceRequestInput{
90-
bookId: String!
88+
bookIds: [String!]!
9189
"""
9290
answer field cannot be PENDING, otherwise it will throw error
9391
"""
@@ -167,21 +165,22 @@ input RegisterServiceInput{
167165
}
168166

169167
type RegisterServiceRespond{
170-
service: [Service]
168+
service: [Service!]
171169
error: GlobalError
172170
}
173171

174172
input UpdateServiceInput{
173+
serviceId: String!
175174
name: String
176175
description: String
177176
startDate: Date
178-
time: Int!
177+
time: Int
179178
active: Boolean
180179
neededAccept: Boolean
181180
}
182181

183182
type UpdateServiceRespond{
184-
service: [Service]
183+
service: [Service!]
185184
error: GlobalError
186185
}
187186

@@ -191,12 +190,12 @@ type RemoveServiceRespond{
191190
}
192191

193192
input BookServiceInput{
194-
serviceId: String!
193+
serviceIds: [String!]!
195194
comments: String
196195
}
197196

198197
type BookServiceRespond{
199-
book: BookingRecord
198+
books: [BookingRecord!]
200199
error: GlobalError
201200
}
202201

@@ -207,7 +206,7 @@ type UserServiceRespond{
207206

208207
type Service implements dbEssentials{
209208
name: String!
210-
description: String!
209+
description: String
211210
ownerId: String!
212211
"""
213212
this field capture time, system does not recognize units, so be consent with your behavior

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
3-
import { GlobalError, errMiddleware } from '../utils/middleware.js';
3+
import { GlobalError, convertDatedObjToString, errMiddleware } from '../utils/middleware.js';
44
import { orm } from '../utils/db/orm.js';
55

66
export const getService = async (input: FieldResolveInput) =>
77
resolverFor('PublicQuery', 'getService', async (args) =>
88
errMiddleware(async () => {
99
return await orm()
1010
.then(async (o) => ({
11-
service: await o('Services').collection.findOne({ _id: args.serviceId, active: { $ne: false } }),
11+
service: convertDatedObjToString(await o('Services').collection.findOne({ _id: args.serviceId, active: { $ne: false } })),
1212
}))
1313
.catch((r) => {
1414
throw new GlobalError(r, import.meta.url);

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

+4-4
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 { errMiddleware } from '../utils/middleware.js';
3+
import { convertDateObjToStringForArray, errMiddleware } from '../utils/middleware.js';
44
import { orm, preparePageOptions } from '../utils/db/orm.js';
55
import { ServicesCollection } from '../utils/db/collections.js';
66

@@ -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: await o(ServicesCollection)
22+
return await orm().then(async (o) => ({
23+
services: convertDateObjToStringForArray(await o(ServicesCollection)
2424
.collection.find({
2525
...pa,
2626
...(fromDate && { createdAt: { $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

+25-14
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,45 @@ 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';
56

67
export const bookService = async (input: FieldResolveInput) =>
78
resolverFor('UserMutation', 'bookService', async (args, src) =>
89
errMiddleware(async () => {
910
sourceContainUserIdOrThrow(src);
1011
const o = await orm();
11-
const service = await o('Services').collection.findOneAndUpdate(
12-
{ _id: args.input.serviceId, taken: { $ne: true } },
12+
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 } },
1317
{ $set: { taken: true } },
1418
);
1519
if (!service.value) {
16-
throw new GlobalError(`service is already taken: ${args.input.serviceId}`, import.meta.url);
20+
throw new GlobalError(`service is already taken: ${serviceId}`, import.meta.url);
1721
}
18-
const book = await o('Bookings')
19-
.createWithAutoFields(
20-
'_id',
21-
'createdAt',
22-
)({
22+
console.log(service);
23+
24+
return service.value
25+
}))
26+
console.log(services);
27+
28+
const books = await o('Bookings')
29+
.collection.insertMany(services.map((service) =>(
30+
{
31+
_id: new ObjectId().toHexString(),
32+
createdAt: new Date(),
2333
bookerId: src.userId,
24-
service: args.input.serviceId,
34+
service: service._id ,
2535
comments: args.input.comments ? args.input.comments : undefined,
26-
status: service.value.neededAccept ? BookStatus.PENDING : BookStatus.ACCEPTED,
27-
})
28-
.then(async (c) => await o('Bookings').collection.findOne({ _id: c.insertedId }));
29-
if (!book) {
36+
status: service.neededAccept ? BookStatus.PENDING : BookStatus.ACCEPTED,
37+
})))
38+
.then(async (c) => o('Bookings').collection.find({ _id: { $in: Object.values(c.insertedIds)} })?.toArray());
39+
if (!books) {
3040
throw new GlobalError('inserted document is null', import.meta.url);
3141
}
32-
return { book: { ...book, service: service.value } };
42+
console.log(books);
43+
return { books: await o('Bookings').composeRelated(books, 'service', 'Services', '_id') };
3344
}),
3445
)(input.arguments, input.source);
3546
export default bookService;
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
3-
import { GlobalError, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
4-
import { mustFindOne, orm } from '../utils/db/orm.js';
3+
import { GlobalError, convertDateObjToStringForArray, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
4+
import { mustFindAny, orm } from '../utils/db/orm.js';
55
import { ServicesCollection } from '../utils/db/collections.js';
6+
import { ServiceModel } from '../models/ServiceModel.js';
7+
import { ObjectId } from 'mongodb';
68

79
export const registerService = async (input: FieldResolveInput) =>
810
resolverFor('UserMutation', 'registerService', async (args, src) =>
911
errMiddleware(async () => {
1012
sourceContainUserIdOrThrow(src);
11-
12-
const servicesPromises = args.input.startDates.map(async startDate => {
13-
13+
const services = args.input.startDates.map((startDate) => {
1414
if (new Date(String(startDate)) < new Date()) {
1515
throw new GlobalError('start date cannot start in past', import.meta.url);
1616
}
17-
const c = await orm().then((o) =>
18-
o(ServicesCollection).createWithAutoFields(
19-
'_id',
20-
'createdAt',
21-
)({
17+
return {
18+
_id: new ObjectId().toHexString(),
19+
createdAt: new Date(),
2220
startDate: new Date(String(startDate)),
2321
name: args.input.name,
2422
neededAccept: args.input.neededAccept === false ? false : true,
2523
description: args.input.description,
2624
ownerId: src.userId || src._id,
2725
active: true,
2826
time: args.input.time,
29-
}),
27+
}
28+
})
29+
const insert = await orm().then((o) =>
30+
o(ServicesCollection).collection.insertMany(services),
3031
);
31-
32-
return {
33-
service: await mustFindOne(ServicesCollection, { _id: c.insertedId }),
34-
};
35-
});
36-
const createdServices = await Promise.all(servicesPromises);
37-
return createdServices;
38-
}),
32+
const insertedIdsArray = Object.values(insert.insertedIds);
33+
const createdServices = await mustFindAny(ServicesCollection, { _id: {$in: insertedIdsArray} });
34+
return { service: convertDateObjToStringForArray(createdServices) };
35+
})
3936
)(input.arguments, input.source);
4037
export default registerService;

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ export const respondOnServiceRequest = async (input: FieldResolveInput) =>
1212
}
1313
const o = await orm();
1414
await o('Bookings')
15-
.collection.findOne({ _id: args.input.bookId, answeredAt: { $exists: false } })
15+
.collection.find({ _id: { $in: args.input.bookIds }, answeredAt: { $exists: false } }).toArray()
1616
.then(async (b) => {
17-
if (!b) {
18-
throw new GlobalError(`cannot find book with id: ${args.input.bookId}`, import.meta.url);
17+
if (!b || b.length < 1) {
18+
throw new GlobalError(`cannot find anyone books for id: ${ args.input.bookIds }`, import.meta.url);
1919
}
2020
await o('Services')
21-
.collection.findOne({ _id: b.service, ownerId: src.userId || src._id})
21+
.collection.findOne({ _id: {$in: b.map((b) => b.service)}, ownerId: src.userId || src._id})
2222
.then((r) => {
23-
if (!r) {
23+
if (!r || b.length < 1) {
2424
throw new GlobalError('you can answer only on yours services', import.meta.url);
2525
}
2626
});
2727
});
2828
return await orm().then((o) =>
2929
o('Bookings')
30-
.collection.updateOne(
31-
{ _id: args.input.bookId },
30+
.collection.updateMany(
31+
{ _id: { $in: args.input.bookIds} },
3232
{ $set: { answeredAt: new Date(), status: args.input.answer } },
3333
)
3434
.then((r) => ({ status: r.modifiedCount !== 0 })),

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

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
3-
import { errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
4-
import { orm } from '../utils/db/orm.js';
3+
import { convertDateObjToStringForArray, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
4+
import { mustFindAny, orm } from '../utils/db/orm.js';
5+
import { ServicesCollection } from '../utils/db/collections.js';
56

67
export const updateService = async (input: FieldResolveInput) =>
78
resolverFor('UserMutation', 'updateService', async (args, src) =>
89
errMiddleware(
9-
async () => {
10+
async () => (
1011
sourceContainUserIdOrThrow(src),
11-
Promise.all(args.input.map(async (update) =>
12+
Promise.all(args.input.map(async (updateSet) =>
1213
await orm().then((o) =>
1314
o('Services')
1415
.collection.updateOne(
15-
{ _id: args.serviceId, ownerId: src.userId || src._id, taken: { $ne: true }, active: { $ne: true } },
16-
{
17-
...Object.fromEntries(Object.entries(update).filter((e) => e !== null)),
18-
startDate: new Date(String(update.startDate)),
19-
},
16+
{ _id: updateSet.serviceId, ownerId: src.userId || src._id, taken: { $ne: true }, active: { $ne: true } },
17+
{$set:
18+
{
19+
...Object.fromEntries(Object.entries({...updateSet, startDate: updateSet.startDate? new Date(updateSet.startDate as string) : undefined }).filter((e) => e !== null)),
20+
updatedAt: new Date,
21+
}},
2022
)
21-
.then((u) => u && { service: o('Services').collection.findOne({ _id: args.serviceId }) }),
23+
2224
)
23-
))
24-
},
25+
)).then(async (u) => u && { service: convertDateObjToStringForArray(await mustFindAny(ServicesCollection, { _id: { $in: args.input.map((up)=>up.serviceId) } })) })
26+
),
2527
),
2628

2729
)(input.arguments, input.source);

0 commit comments

Comments
 (0)