-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for as-associate endpoints
This implements the carts and orders endpoints via the as-associate endpoints. It doesn’t do any permission checking for now, just proxies the requests to the existing code. In the future we should filter the responses atleast on the business units
- Loading branch information
1 parent
ca97a89
commit a7166d7
Showing
10 changed files
with
185 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@labdigital/commercetools-mock": minor | ||
--- | ||
|
||
Support as-associate endpoints for carts and orders |
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,5 @@ | ||
import { CartRepository } from "./cart"; | ||
import { OrderRepository } from "./order"; | ||
|
||
export class AsAssociateOrderRepository extends OrderRepository {} | ||
export class AsAssociateCartRepository extends CartRepository {} |
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,33 @@ | ||
import { Router } from "express"; | ||
import type { CartRepository } from "../repositories/cart"; | ||
import AbstractService from "./abstract"; | ||
|
||
export class AsAssociateCartService extends AbstractService { | ||
public repository: CartRepository; | ||
|
||
constructor(parent: Router, repository: CartRepository) { | ||
super(parent); | ||
this.repository = repository; | ||
} | ||
|
||
getBasePath() { | ||
return "carts"; | ||
} | ||
|
||
registerRoutes(parent: Router) { | ||
const basePath = this.getBasePath(); | ||
const router = Router({ mergeParams: true }); | ||
|
||
this.extraRoutes(router); | ||
|
||
router.get("/", this.get.bind(this)); | ||
router.get("/:id", this.getWithId.bind(this)); | ||
|
||
router.delete("/:id", this.deleteWithId.bind(this)); | ||
|
||
router.post("/", this.post.bind(this)); | ||
router.post("/:id", this.postWithId.bind(this)); | ||
|
||
parent.use(`/${basePath}`, router); | ||
} | ||
} |
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,64 @@ | ||
import type { Order } from "@commercetools/platform-sdk"; | ||
import assert from "assert"; | ||
import supertest from "supertest"; | ||
import { afterEach, beforeEach, describe, expect, test } from "vitest"; | ||
import { CommercetoolsMock } from "../index"; | ||
|
||
describe("Order Query", () => { | ||
const ctMock = new CommercetoolsMock(); | ||
let order: Order | undefined; | ||
const projectKey = "dummy"; | ||
const customerId = "5fac8fca-2484-4b14-a1d1-cfdce2f8d3c4"; | ||
const businessUnitKey = "business-unit"; | ||
|
||
beforeEach(async () => { | ||
let response = await supertest(ctMock.app) | ||
.post( | ||
`/${projectKey}/as-associate/${customerId}/in-business-unit/key=${businessUnitKey}/carts`, | ||
) | ||
.send({ | ||
currency: "EUR", | ||
custom: { | ||
type: { | ||
key: "my-cart", | ||
}, | ||
fields: { | ||
description: "example description", | ||
}, | ||
}, | ||
}); | ||
expect(response.status).toBe(201); | ||
const cart = response.body; | ||
|
||
response = await supertest(ctMock.app) | ||
.post( | ||
`/${projectKey}/as-associate/${customerId}/in-business-unit/key=${businessUnitKey}/orders`, | ||
) | ||
.send({ | ||
cart: { | ||
typeId: "cart", | ||
id: cart.id, | ||
}, | ||
orderNumber: "foobar", | ||
}); | ||
expect(response.status).toBe(201); | ||
order = response.body; | ||
}); | ||
|
||
afterEach(() => { | ||
ctMock.clear(); | ||
}); | ||
|
||
test("no filter", async () => { | ||
assert(order, "order not created"); | ||
|
||
const response = await supertest(ctMock.app).get( | ||
`/${projectKey}/as-associate/${customerId}/in-business-unit/key=${businessUnitKey}/orders`, | ||
); | ||
expect(response.status).toBe(200); | ||
expect(response.body.count).toBe(1); | ||
expect(response.body.total).toBe(1); | ||
expect(response.body.offset).toBe(0); | ||
expect(response.body.limit).toBe(20); | ||
}); | ||
}); |
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,33 @@ | ||
import { Router } from "express"; | ||
import type { MyOrderRepository } from "../repositories/my-order"; | ||
import AbstractService from "./abstract"; | ||
|
||
export class AsAssociateOrderService extends AbstractService { | ||
public repository: MyOrderRepository; | ||
|
||
constructor(parent: Router, repository: MyOrderRepository) { | ||
super(parent); | ||
this.repository = repository; | ||
} | ||
|
||
getBasePath() { | ||
return "orders"; | ||
} | ||
|
||
registerRoutes(parent: Router) { | ||
const basePath = this.getBasePath(); | ||
const router = Router({ mergeParams: true }); | ||
|
||
this.extraRoutes(router); | ||
|
||
router.get("/", this.get.bind(this)); | ||
router.get("/:id", this.getWithId.bind(this)); | ||
|
||
router.delete("/:id", this.deleteWithId.bind(this)); | ||
|
||
router.post("/", this.post.bind(this)); | ||
router.post("/:id", this.postWithId.bind(this)); | ||
|
||
parent.use(`/${basePath}`, router); | ||
} | ||
} |
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,34 @@ | ||
import { Router } from "express"; | ||
import type { | ||
AsAssociateCartRepository, | ||
AsAssociateOrderRepository, | ||
} from "~src/repositories/as-associate"; | ||
import { AsAssociateCartService } from "./as-associate-cart"; | ||
import { AsAssociateOrderService } from "./as-associate-order"; | ||
|
||
type Repositories = { | ||
cart: AsAssociateCartRepository; | ||
order: AsAssociateOrderRepository; | ||
}; | ||
|
||
export class AsAssociateService { | ||
router: Router; | ||
|
||
subServices: { | ||
cart: AsAssociateCartService; | ||
order: AsAssociateOrderService; | ||
}; | ||
|
||
constructor(parent: Router, repositories: Repositories) { | ||
this.router = Router({ mergeParams: true }); | ||
|
||
this.subServices = { | ||
order: new AsAssociateOrderService(this.router, repositories.order), | ||
cart: new AsAssociateCartService(this.router, repositories.cart), | ||
}; | ||
parent.use( | ||
"/as-associate/:associateId/in-business-unit/key=:businessUnitId", | ||
this.router, | ||
); | ||
} | ||
} |
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