-
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.
Add customer blocked services with create, destroy, and range functio…
…nality and tests
- Loading branch information
1 parent
9ade233
commit b11dea7
Showing
9 changed files
with
205 additions
and
28 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,22 @@ | ||
import { createUser } from "~/library/jest/helpers"; | ||
import { CustomerBlockedServiceCreate } from "./create"; | ||
require("~/library/jest/mongoose/mongodb.jest"); | ||
|
||
describe("CustomerBlockedServiceCreate", () => { | ||
it("should create blocked document", async () => { | ||
const customer = await createUser({ customerId: 7106990342471 }); | ||
|
||
const start = new Date("2023-11-26T00:00:00+03:00"); | ||
const end = new Date("2024-01-07T00:00:00+03:00"); | ||
|
||
const blockedDocuments = await CustomerBlockedServiceCreate({ | ||
customerId: 7106990342471, | ||
start, | ||
end, | ||
}); | ||
|
||
expect(blockedDocuments.customerId).toEqual(7106990342471); | ||
expect(blockedDocuments.start).toEqual(start); | ||
expect(blockedDocuments.end).toEqual(end); | ||
}); | ||
}); |
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,7 @@ | ||
import { BlockedModel } from "~/functions/blocked/blocked.model"; | ||
import { Blocked } from "~/functions/blocked/blocked.types"; | ||
|
||
export const CustomerBlockedServiceCreate = (props: Blocked) => { | ||
const created = new BlockedModel(props); | ||
return created.save(); | ||
}; |
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,26 @@ | ||
import { createUser } from "~/library/jest/helpers"; | ||
import { createBlocked } from "~/library/jest/helpers/blocked"; | ||
import { CustomerBlockedServiceDestroy } from "./destroy"; | ||
require("~/library/jest/mongoose/mongodb.jest"); | ||
|
||
describe("CustomerBlockedServiceDestroy", () => { | ||
it("should destroy blocked document", async () => { | ||
const customer = await createUser({ customerId: 7106990342471 }); | ||
|
||
const start = new Date("2023-11-26T00:00:00+03:00"); | ||
const end = new Date("2024-01-07T00:00:00+03:00"); | ||
|
||
const blocked = await createBlocked({ | ||
customerId: 7106990342471, | ||
start, | ||
end, | ||
}); | ||
|
||
const blockedDocuments = await CustomerBlockedServiceDestroy({ | ||
blockedId: blocked._id, | ||
customerId: customer.customerId, | ||
}); | ||
|
||
expect(blockedDocuments.deletedCount).toBe(1); | ||
}); | ||
}); |
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,17 @@ | ||
import { BlockedModel } from "~/functions/blocked/blocked.model"; | ||
import { StringOrObjectId } from "~/library/zod"; | ||
|
||
export type CustomerBlockedServiceDestroyProps = { | ||
blockedId: StringOrObjectId; | ||
customerId: number; | ||
}; | ||
|
||
export const CustomerBlockedServiceDestroy = ({ | ||
blockedId, | ||
customerId, | ||
}: CustomerBlockedServiceDestroyProps) => { | ||
return BlockedModel.deleteOne({ | ||
_id: blockedId, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { isWithinInterval } from "date-fns"; | ||
import { createUser } from "~/library/jest/helpers"; | ||
import { createBlocked } from "~/library/jest/helpers/blocked"; | ||
import { CustomerBlockedServiceRange } from "./range"; | ||
require("~/library/jest/mongoose/mongodb.jest"); | ||
|
||
describe("CustomerBlockedServiceRange", () => { | ||
beforeAll(async () => { | ||
await createBlocked({ | ||
customerId: 7106990342471, | ||
start: new Date("2023-11-25"), | ||
end: new Date("2023-11-27"), | ||
}); | ||
await createBlocked({ | ||
customerId: 7106990342471, | ||
start: new Date("2023-12-15"), | ||
end: new Date("2023-12-20"), | ||
}); | ||
// outside range | ||
await createBlocked({ | ||
customerId: 7106990342471, | ||
start: new Date("2024-01-08"), | ||
end: new Date("2024-01-10"), | ||
}); | ||
}); | ||
|
||
it("should return blockeds for customer", async () => { | ||
const customer = await createUser({ customerId: 7106990342471 }); | ||
|
||
const start = new Date("2023-11-26T00:00:00+03:00"); | ||
const end = new Date("2024-01-07T00:00:00+03:00"); | ||
const blockedDocuments = await CustomerBlockedServiceRange({ | ||
customerId: customer.customerId, | ||
start, | ||
end, | ||
}); | ||
|
||
const range = { start, end }; | ||
const allInOrIntersectRange = blockedDocuments.every( | ||
(doc) => | ||
isWithinInterval(new Date(doc.start), range) || | ||
isWithinInterval(new Date(doc.end), range) || | ||
(new Date(doc.start) < start && new Date(doc.end) > end) | ||
); | ||
|
||
expect(blockedDocuments.length).toBe(2); | ||
expect(allInOrIntersectRange).toBe(true); | ||
}); | ||
}); |
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,50 @@ | ||
import { BlockedModel } from "~/functions/blocked/blocked.model"; | ||
|
||
export type CustomerBlockedServiceRangeProps = { | ||
customerId: number; | ||
start: string | Date; | ||
end: string | Date; | ||
}; | ||
|
||
export type CustomerBlockedServiceRangeAggregate = { | ||
start: Date; | ||
end: Date; | ||
title: string; | ||
}; | ||
|
||
export const CustomerBlockedServiceRange = async ({ | ||
customerId, | ||
start: startDate, | ||
end: endDate, | ||
}: CustomerBlockedServiceRangeProps) => { | ||
const start = new Date(startDate); | ||
const end = new Date(endDate); | ||
|
||
return BlockedModel.aggregate<CustomerBlockedServiceRangeAggregate>([ | ||
{ | ||
$match: { | ||
$and: [ | ||
{ | ||
customerId, | ||
}, | ||
{ | ||
$or: [ | ||
{ | ||
start: { | ||
$gte: start, | ||
$lte: end, | ||
}, | ||
}, | ||
{ | ||
end: { | ||
$gte: start, | ||
$lte: end, | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
]); | ||
}; |
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,17 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { BlockedModel } from "~/functions/blocked/blocked.model"; | ||
import { Blocked } from "~/functions/blocked/blocked.types"; | ||
|
||
export const getBlockedObject = ( | ||
props: Partial<Blocked> = {} | ||
): Omit<Blocked, "_id"> => ({ | ||
start: faker.date.past(), | ||
end: faker.date.future(), | ||
customerId: 1, | ||
...props, | ||
}); | ||
|
||
export const createBlocked = (props: Blocked) => { | ||
const blocked = new BlockedModel(getBlockedObject({ ...props })); | ||
return blocked.save(); | ||
}; |