Skip to content

Commit 5f91532

Browse files
committed
refactorFiltersAndAddFiltersForBooks
1 parent 0f7d777 commit 5f91532

File tree

7 files changed

+46
-43
lines changed

7 files changed

+46
-43
lines changed

packages/integrations/gei-bookings/package.json

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

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

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const getService = async (input: FieldResolveInput) =>
1111
throw new GlobalError(r, import.meta.url);
1212
}))
1313
})
14-
1514
}),
1615
)(input.arguments);
1716
export default getService;

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

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
33
import { convertDateObjToStringForArray, errMiddleware } from '../utils/middleware.js';
4-
import { MongoOrb, preparePageOptions } from '../utils/db/orm.js';
4+
import { MongoOrb, inputServiceFiltersSet, 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);
88
export const listServices = async (input: FieldResolveInput) =>
99
resolverFor('PublicQuery', 'listServices', async (args) =>
1010
errMiddleware(async () => {
1111
const po = preparePageOptions(args?.input?.page);
12-
const pa =
13-
args?.input?.filters &&
14-
Object.fromEntries(Object.entries(args?.input?.filters).filter((v) => v !== null && v !== undefined && v[0] !== 'fromDate' && v[0] !== 'toDate'));
15-
const fromDate = isScalarDate(args?.input?.filters?.fromDate)
16-
? isScalarDate(args?.input?.filters?.fromDate)
17-
: undefined;
18-
19-
const toDate = isScalarDate(args?.input?.filters?.toDate)
20-
? isScalarDate(args?.input?.filters?.toDate)
21-
: undefined;
12+
const inputFilters = inputServiceFiltersSet(args.input?.filters)
2213
return {
2314
services: convertDateObjToStringForArray(await MongoOrb(ServicesCollection)
2415
.collection.find({
25-
...pa,
26-
...(fromDate && { startDate: { $gte: new Date(args?.input?.filters?.fromDate as string) } }),
27-
...(toDate && { startDate: { $lte: new Date(args?.input?.filters?.toDate as string) } }),
28-
...(args?.input?.filters?.name && { name: { $regex: args?.input.filters.name, $options: 'i' } }),
29-
...(args?.input?.filters?.description && {
30-
description: { $regex: args?.input.filters.description, $options: 'i' },
31-
}),
16+
...inputFilters,
3217
active: { $ne: false },
3318
taken: { $ne: true },
3419
})

packages/integrations/gei-bookings/src/UserQuery/getBookingsForService.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
33
import { sourceContainUserIdOrThrow, errMiddleware, convertDateObjToStringForArray } from '../utils/middleware.js';
4-
import { MongoOrb, preparePageOptions } from '../utils/db/orm.js';
4+
import { MongoOrb, inputBooksFiltersSet, preparePageOptions } from '../utils/db/orm.js';
55
import { BookingRecordModel } from '../models/BookingRecordModel.js';
66

77
export const getBookingsForService = async (input: FieldResolveInput) =>
88
resolverFor('UserQuery', 'getBookingsForService', async (args, src) =>
99
errMiddleware(async () => {
1010
sourceContainUserIdOrThrow(src);
1111
const po = preparePageOptions(args?.input?.page);
12+
const inputFilters = inputBooksFiltersSet(args.input?.filters)
1213

1314

1415
const ownedServices = await MongoOrb('Services')
@@ -17,7 +18,7 @@ export const getBookingsForService = async (input: FieldResolveInput) =>
1718
.then((s) => s.map((ss) => ss._id))
1819

1920
const bookings = await MongoOrb('Bookings')
20-
.collection.find({ services: { $in: ownedServices } })
21+
.collection.find({ ...inputFilters, services: { $in: ownedServices } })
2122
.limit(po.limit)
2223
.skip(po.skip)
2324
.sort('createdAt', -1)

packages/integrations/gei-bookings/src/UserQuery/getSelfBooks.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { FieldResolveInput } from 'stucco-js';
22
import { resolverFor } from '../zeus/index.js';
3-
import { MongoOrb, preparePageOptions } from '../utils/db/orm.js';
3+
import { MongoOrb, inputBooksFiltersSet, preparePageOptions } from '../utils/db/orm.js';
44
import { convertDateObjToStringForArray, errMiddleware, sourceContainUserIdOrThrow } from '../utils/middleware.js';
55

66
export const getSelfBooks = async (input: FieldResolveInput) =>
77
resolverFor('UserQuery', 'getSelfBooks', async (args, src) =>
88
errMiddleware(async () => {
99
sourceContainUserIdOrThrow(src);
1010
const po = preparePageOptions(args?.input?.page);
11+
const inputFilters = inputBooksFiltersSet(args.input?.filters)
12+
1113
return {
1214
books: convertDateObjToStringForArray(await MongoOrb('Bookings')
13-
.collection.find({ bookerId: src.userId || src._id })
15+
.collection.find({ ...inputFilters, bookerId: src.userId || src._id })
1416
.skip(po.skip)
1517
.limit(po.limit)
1618
.sort('createdAt', -1)
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, sourceContainUserIdOrThrow } from '../utils/middleware.js';
4-
import { MongoOrb, preparePageOptions } from '../utils/db/orm.js';
4+
import { MongoOrb, inputServiceFiltersSet, preparePageOptions } from '../utils/db/orm.js';
55
import { ServicesCollection } from '../utils/db/collections.js';
66
import { isScalarDate } from '../PublicQuery/listServices.js';
77
import { ServiceModel } from '../models/ServiceModel.js';
@@ -12,25 +12,12 @@ export const getSelfServices = async (input: FieldResolveInput) =>
1212
errMiddleware(async () => {
1313
sourceContainUserIdOrThrow(src);
1414
const po = preparePageOptions(args?.input?.page);
15-
const pa =
16-
args?.input?.filters &&
17-
Object.fromEntries(Object.entries(args?.input?.filters).filter((v) => v !== null && v !== undefined && v[0] !== 'fromDate' && v[0] !== 'toDate'));
18-
const fromDate = isScalarDate(args?.input?.filters?.fromDate)
19-
? isScalarDate(args?.input?.filters?.fromDate)
20-
: undefined;
21-
22-
const toDate = isScalarDate(args?.input?.filters?.toDate)
23-
? isScalarDate(args?.input?.filters?.toDate)
24-
: undefined;
15+
16+
const inputFilters = inputServiceFiltersSet(args.input?.filters)
17+
2518
const selfServices = await MongoOrb(ServicesCollection)
2619
.collection.find({
27-
...pa,
28-
...(fromDate && { startDate: { $gte: new Date(args?.input?.filters?.fromDate as string) } }),
29-
...(toDate && { startDate: { $lte: new Date(args?.input?.filters?.toDate as string) } }),
30-
...(args?.input?.filters?.name && { name: { $regex: args?.input.filters.name, $options: 'i' } }),
31-
...(args?.input?.filters?.description && {
32-
description: { $regex: args?.input.filters.description, $options: 'i' },
33-
}),
20+
...inputFilters,
3421
ownerId: src.userId || src._id,
3522
})
3623
.limit(po.limit)
@@ -41,3 +28,6 @@ export const getSelfServices = async (input: FieldResolveInput) =>
4128
}),
4229
)(input.arguments, input.source);
4330
export default getSelfServices;
31+
32+
33+

packages/integrations/gei-bookings/src/utils/db/orm.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,30 @@ export function updateNestedFields(inputObject: Record<string, any>, nestedObjec
9191
}
9292
}
9393
return updateObject;
94-
}
94+
}
95+
96+
export const isScalarDate = (obj: unknown): boolean => typeof obj === 'string' && obj !== null && !!Date.parse(obj);
97+
export const inputServiceFiltersSet = (filters: any ) => {
98+
if (!filters) return {}
99+
const pa =
100+
filters &&
101+
Object.fromEntries(Object.entries(filters).filter((v) => v !== null && v !== undefined && v[0] !== 'fromDate' && v[0] !== 'toDate'));
102+
const fromDate = isScalarDate(filters.fromDate)
103+
const toDate = isScalarDate(filters.toDate)
104+
105+
return {...pa, ...(toDate && { startDate: { $lte: new Date(filters.toDate as string) } }), ...(fromDate && { startDate: { $gte: new Date(filters.fromDate as string) } }), ...(filters?.name && { name: { $regex: filters.name, $options: 'i' } }),
106+
...(filters?.description && {
107+
description: { $regex: filters.description, $options: 'i' },
108+
})}
109+
}
110+
111+
112+
export const inputBooksFiltersSet = (filters: any ) => {
113+
if (!filters) return {}
114+
const pa =
115+
filters &&
116+
Object.fromEntries(Object.entries(filters).filter((v) => v !== null && v !== undefined && v[0] !== 'fromDate' && v[0] !== 'toDate'));
117+
const fromDate = isScalarDate(filters.fromDate)
118+
const toDate = isScalarDate(filters.toDate)
119+
return {...pa, ...(toDate && { 'comments.to': { $lte: new Date(filters.toDate as string) } }), ...(fromDate && { 'comments.from': { $gte: new Date(filters.fromDate as string) } })}}
120+

0 commit comments

Comments
 (0)