-
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.
fix(openapi): change parameter name from 'productId' to 'productHandl…
…e' in openapi.yaml and index.yaml feat(schedule.types): add 'productHandle' property to ScheduleProductZodSchema fix(product.schema): add 'productHandle' field to ProductSchema fix(user-product.function): change parameter name from 'productId' to 'productHandle' in route definition fix(user-product.function): change parameter name from 'productId' to 'productHandle' in request query fix(user-product.function): change parameter name from 'productId' to 'productHandle' in controller feat(user-product.function): create UserProductServiceGet function to retrieve product by productHandle feat(user-product.function): add UserProductsControllerGet handler to call UserProductServiceGet feat(user-product.function): add UserProductsControllerGetQuerySchema to validate request query feat(user-product.function): update UserProductsControllerGet to use UserProductServiceGet feat(user-product.function): add UserProductsController
- Loading branch information
1 parent
4b68efe
commit f682000
Showing
10 changed files
with
122 additions
and
19 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
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
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
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,32 +1,24 @@ | ||
import { z } from "zod"; | ||
import { CustomerProductServiceGet } from "~/functions/customer/services/product"; | ||
|
||
import { ScheduleProductZodSchema } from "~/functions/schedule/schedule.types"; | ||
import { _ } from "~/library/handler"; | ||
import { UserServiceGetCustomerId } from "../../services/user"; | ||
import { UserProductServiceGet } from "../../services/products/get"; | ||
|
||
export type UserProductsControllerGetRequest = { | ||
query: z.infer<typeof UserProductsControllerGetQuerySchema>; | ||
}; | ||
|
||
const UserProductsControllerGetQuerySchema = z.object({ | ||
username: z.string(), | ||
productId: ScheduleProductZodSchema.shape.productId, | ||
productHandle: z.string(), | ||
}); | ||
|
||
export type UserProductsControllerGetResponse = Awaited< | ||
ReturnType<typeof CustomerProductServiceGet> | ||
ReturnType<typeof UserProductServiceGet> | ||
>; | ||
|
||
export const UserProductsControllerGet = _( | ||
async ({ query }: UserProductsControllerGetRequest) => { | ||
const validateQuery = UserProductsControllerGetQuerySchema.parse(query); | ||
const user = await UserServiceGetCustomerId({ | ||
username: validateQuery.username, | ||
}); | ||
return CustomerProductServiceGet({ | ||
customerId: user.customerId, | ||
productId: validateQuery.productId, | ||
}); | ||
return UserProductServiceGet(validateQuery); | ||
} | ||
); |
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,51 @@ | ||
import { CustomerProductServiceUpsert } from "~/functions/customer/services/product"; | ||
import { CustomerScheduleServiceCreate } from "~/functions/customer/services/schedule/create"; | ||
import { TimeUnit } from "~/functions/schedule"; | ||
import { createUser } from "~/library/jest/helpers"; | ||
import { getProductObject } from "~/library/jest/helpers/product"; | ||
import { UserProductServiceGet } from "./get"; | ||
|
||
require("~/library/jest/mongoose/mongodb.jest"); | ||
|
||
describe("UserProductsService", () => { | ||
const name = "Test Schedule"; | ||
const productId = 1000; | ||
const newProduct = getProductObject({ | ||
variantId: 1, | ||
duration: 60, | ||
breakTime: 0, | ||
noticePeriod: { | ||
value: 1, | ||
unit: TimeUnit.DAYS, | ||
}, | ||
bookingPeriod: { | ||
value: 1, | ||
unit: TimeUnit.WEEKS, | ||
}, | ||
locations: [], | ||
}); | ||
|
||
it("should find a product", async () => { | ||
const user = await createUser({ customerId: 134 }); | ||
|
||
const newSchedule = await CustomerScheduleServiceCreate({ | ||
name, | ||
customerId: user.customerId, | ||
}); | ||
|
||
const updatedSchedule = await CustomerProductServiceUpsert( | ||
{ | ||
customerId: newSchedule.customerId, | ||
productId, | ||
}, | ||
{ ...newProduct, scheduleId: newSchedule._id } | ||
); | ||
|
||
const foundProduct = await UserProductServiceGet({ | ||
username: user.username || "", | ||
productHandle: newProduct.productHandle, | ||
}); | ||
|
||
expect(foundProduct).toMatchObject({ 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,55 @@ | ||
import { ScheduleModel } from "~/functions/schedule"; | ||
import { NotFoundError } from "~/library/handler"; | ||
import { UserServiceGetCustomerId } from "../user"; | ||
|
||
export type UserProductServiceGetFilter = { | ||
username: string; | ||
productHandle: string; | ||
}; | ||
|
||
export const UserProductServiceGet = async ( | ||
filter: UserProductServiceGetFilter | ||
) => { | ||
const user = await UserServiceGetCustomerId({ | ||
username: filter.username, | ||
}); | ||
|
||
const schedule = await ScheduleModel.findOne({ | ||
customerId: user.customerId, | ||
products: { | ||
$elemMatch: { | ||
productHandle: filter.productHandle, | ||
}, | ||
}, | ||
}) | ||
.orFail( | ||
new NotFoundError([ | ||
{ | ||
code: "custom", | ||
message: "PRODUCT_NOT_FOUND", | ||
path: ["customerId", "productHandle"], | ||
}, | ||
]) | ||
) | ||
.lean(); | ||
|
||
const product = schedule.products.find( | ||
(p) => p.productHandle === filter.productHandle | ||
); | ||
|
||
if (!product) { | ||
throw new NotFoundError([ | ||
{ | ||
code: "custom", | ||
message: "PRODUCT_NOT_FOUND", | ||
path: ["productHandle"], | ||
}, | ||
]); | ||
} | ||
|
||
return { | ||
...product, | ||
scheduleId: schedule._id, | ||
scheduleName: schedule.name, | ||
}; | ||
}; |
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