-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from jamalsoueidan/user-schedule-get-by-produc…
…t-id feat(openapi): add UserScheduleGetByProductIdResponse to openapi.yaml
- Loading branch information
Showing
15 changed files
with
468 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
get: | ||
parameters: | ||
- name: username | ||
in: path | ||
description: username | ||
required: true | ||
schema: | ||
type: string | ||
- name: productId | ||
in: path | ||
description: product Id | ||
required: true | ||
schema: | ||
type: string | ||
tags: | ||
- UserSchedule | ||
operationId: userScheduleGetByProductId | ||
summary: GET Get user schedule | ||
description: This endpoint gets user schedule object by product id | ||
responses: | ||
"200": | ||
description: Response with payload | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "./response.yaml" | ||
|
||
"400": | ||
$ref: "../../../../responses/bad.yaml" | ||
"401": | ||
$ref: "../../../../responses/unauthorized.yaml" | ||
"403": | ||
$ref: "../../../../responses/forbidden.yaml" | ||
"404": | ||
$ref: "../../../../responses/not-found.yaml" | ||
|
||
security: [] |
18 changes: 18 additions & 0 deletions
18
openapi/paths/user/schedule/get-by-product-id/response.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
type: object | ||
properties: | ||
success: | ||
type: boolean | ||
example: true | ||
payload: | ||
allOf: | ||
- $ref: ../_types/schedule-with-locations.yaml | ||
- type: object | ||
properties: | ||
product: | ||
$ref: ../_types/product-with-locations.yaml | ||
required: | ||
- product | ||
|
||
required: | ||
- success | ||
- payload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { z } from "zod"; | ||
|
||
import { _ } from "~/library/handler"; | ||
import { NumberOrStringType } from "~/library/zod"; | ||
import { UserScheduleServiceGetByProductId } from "../../services/schedule/get-by-product"; | ||
import { UserServiceGetCustomerId } from "../../services/user"; | ||
|
||
export type UserScheduleControllerGetByProductIdRequest = { | ||
query: z.infer<typeof UserScheduleControllerGetByProductIdQuerySchema>; | ||
}; | ||
|
||
const UserScheduleControllerGetByProductIdQuerySchema = z.object({ | ||
username: z.string(), | ||
productId: NumberOrStringType, | ||
}); | ||
|
||
export type UserScheduleControllerGetByProductIdResponse = Awaited< | ||
ReturnType<typeof UserScheduleServiceGetByProductId> | ||
>; | ||
|
||
export const UserScheduleControllerGetByProductId = _( | ||
async ({ query }: UserScheduleControllerGetByProductIdRequest) => { | ||
const validateQuery = | ||
UserScheduleControllerGetByProductIdQuerySchema.parse(query); | ||
const user = await UserServiceGetCustomerId(validateQuery); | ||
return UserScheduleServiceGetByProductId({ | ||
productId: validateQuery.productId, | ||
customerId: user.customerId, | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 8 additions & 9 deletions
17
...nctions/user/controllers/schedule/list.ts → ...er/controllers/schedule/locations-list.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
import { z } from "zod"; | ||
|
||
import { _ } from "~/library/handler"; | ||
import { UserScheduleServiceLocationsList } from "../../services/schedule"; | ||
import { UserScheduleServiceLocationsList } from "../../services/schedule/locations/locations-list"; | ||
import { UserServiceGet } from "../../services/user"; | ||
|
||
export type UserScheduleControllerListRequest = { | ||
query: z.infer<typeof UserScheduleControllerListQuerySchema>; | ||
export type UserScheduleControllerLocationsListRequest = { | ||
query: z.infer<typeof UserScheduleControllerLocationsListQuerySchema>; | ||
}; | ||
|
||
const UserScheduleControllerListQuerySchema = z.object({ | ||
const UserScheduleControllerLocationsListQuerySchema = z.object({ | ||
username: z.string(), | ||
}); | ||
|
||
export type UserScheduleControllerListResponse = Awaited< | ||
ReturnType<typeof UserScheduleServiceLocationsList> | ||
>; | ||
|
||
export const UserScheduleControllerList = _( | ||
async ({ query }: UserScheduleControllerListRequest) => { | ||
const validateQuery = UserScheduleControllerListQuerySchema.parse(query); | ||
|
||
export const UserScheduleControllerLocationsList = _( | ||
async ({ query }: UserScheduleControllerLocationsListRequest) => { | ||
const validateQuery = | ||
UserScheduleControllerLocationsListQuerySchema.parse(query); | ||
const { customerId } = await UserServiceGet(validateQuery); | ||
|
||
return UserScheduleServiceLocationsList({ customerId }); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/functions/user/services/schedule/get-by-product.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { CustomerScheduleServiceCreate } from "~/functions/customer/services/schedule/create"; | ||
import { createUser } from "~/library/jest/helpers"; | ||
import { createLocation } from "~/library/jest/helpers/location"; | ||
import { getProductObject } from "~/library/jest/helpers/product"; | ||
import { UserScheduleServiceGetByProductId } from "./get-by-product"; | ||
|
||
require("~/library/jest/mongoose/mongodb.jest"); | ||
|
||
describe("UserScheduleServiceGetByProductId", () => { | ||
const customerId = 123; | ||
const username = "test"; | ||
|
||
beforeEach(() => createUser({ customerId }, { username })); | ||
|
||
it("should get schedule with products only belongs to specific locationId", async () => { | ||
const locationOrigin = await createLocation({ customerId }); | ||
|
||
const product = getProductObject({ | ||
locations: [ | ||
{ | ||
location: locationOrigin._id, | ||
locationType: locationOrigin.locationType, | ||
}, | ||
], | ||
}); | ||
const schedule = await CustomerScheduleServiceCreate({ | ||
name: faker.person.firstName(), | ||
customerId, | ||
products: [ | ||
product, | ||
getProductObject({ | ||
locations: [ | ||
{ | ||
location: locationOrigin._id, | ||
locationType: locationOrigin.locationType, | ||
}, | ||
], | ||
}), | ||
getProductObject({ | ||
locations: [ | ||
{ | ||
location: locationOrigin._id, | ||
locationType: locationOrigin.locationType, | ||
}, | ||
], | ||
}), | ||
], | ||
}); | ||
|
||
// another fake schedule | ||
await CustomerScheduleServiceCreate({ | ||
name: faker.person.firstName(), | ||
customerId, | ||
products: [ | ||
getProductObject({ | ||
locations: [ | ||
{ | ||
location: locationOrigin._id, | ||
locationType: locationOrigin.locationType, | ||
}, | ||
], | ||
}), | ||
], | ||
}); | ||
|
||
const getSchedule = await UserScheduleServiceGetByProductId({ | ||
customerId: schedule.customerId, | ||
productId: product.productId, | ||
}); | ||
|
||
expect(getSchedule!.name).toBe(schedule.name); | ||
expect(getSchedule!._id.toString()).toBe(schedule._id.toString()); | ||
expect(getSchedule!.locations.length).toBe(1); | ||
expect(getSchedule!.product.productId).toBe(product.productId); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Schedule, ScheduleModel, ScheduleProduct } from "~/functions/schedule"; | ||
import { NotFoundError } from "~/library/handler"; | ||
|
||
export type UesrScheduleAggreateReturnValue = Pick< | ||
Schedule, | ||
"_id" | "customerId" | "name" | ||
> & { product: ScheduleProduct } & { locations: Array<Location> }; | ||
|
||
export type UserScheduleServiceGetByProductIdProps = { | ||
customerId: number; | ||
productId: number; | ||
}; | ||
|
||
export async function UserScheduleServiceGetByProductId({ | ||
customerId, | ||
productId, | ||
}: UserScheduleServiceGetByProductIdProps) { | ||
// first just the schedule with the product | ||
const schedule = await ScheduleModel.findOne({ | ||
customerId, | ||
"products.productId": productId, | ||
}).orFail( | ||
new NotFoundError([ | ||
{ | ||
path: ["customerId", "productId"], | ||
message: "NOT_FOUND", | ||
code: "custom", | ||
}, | ||
]) | ||
); | ||
|
||
const schedules = | ||
await ScheduleModel.aggregate<UesrScheduleAggreateReturnValue>([ | ||
{ | ||
$match: { | ||
_id: schedule._id, | ||
customerId, | ||
}, | ||
}, | ||
{ | ||
$unwind: "$products", | ||
}, | ||
{ | ||
$match: { | ||
"products.productId": productId, | ||
}, | ||
}, | ||
{ | ||
$unwind: "$products.locations", | ||
}, | ||
{ | ||
$lookup: { | ||
from: "Location", | ||
localField: "products.locations.location", | ||
foreignField: "_id", | ||
as: "products.locations", | ||
}, | ||
}, | ||
{ $unwind: "$products.locations" }, | ||
{ | ||
$group: { | ||
_id: "$_id", | ||
locations: { | ||
$addToSet: "$products.locations", | ||
}, | ||
product: { $first: "$products" }, | ||
name: { $first: "$name" }, | ||
slots: { $first: "$slots" }, | ||
createdAt: { $first: "$createdAt" }, | ||
updatedAt: { $first: "$updatedAt" }, | ||
}, | ||
}, | ||
]); | ||
|
||
if (schedules.length === 0) { | ||
throw new NotFoundError([ | ||
{ | ||
path: ["customerId", "scheduleId", "locationId"], | ||
message: "NOT_FOUND", | ||
code: "custom", | ||
}, | ||
]); | ||
} | ||
|
||
return schedules[0]; | ||
} |
Oops, something went wrong.