-
Notifications
You must be signed in to change notification settings - Fork 0
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 #251 from litespace-org/moehab/availability-slot
added: AvailabilitySlot table/model basic functionality and test units.
- Loading branch information
Showing
9 changed files
with
333 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { IAvailabilitySlot } from "@litespace/types"; | ||
import dayjs from "@/lib/dayjs"; | ||
import { Knex } from "knex"; | ||
import { first, isEmpty } from "lodash"; | ||
import { knex, column, WithOptionalTx } from "@/query"; | ||
|
||
type SearchFilter = { | ||
/** | ||
* User ids to be included in the search query. | ||
*/ | ||
users?: number[]; | ||
/** | ||
* Start date time (ISO datetime format) | ||
* All slots after (or the same as) this date will be included. | ||
*/ | ||
after?: string; | ||
/** | ||
* End date time (ISO datetime format) | ||
* All slots before (or the same as) this date will be included. | ||
*/ | ||
before?: string; | ||
}; | ||
|
||
export class AvailabilitySlots { | ||
table = "availability_slots" as const; | ||
|
||
async create( | ||
payloads: IAvailabilitySlot.CreatePayload[], | ||
tx?: Knex.Transaction | ||
): Promise<IAvailabilitySlot.Self[]> { | ||
if (isEmpty(payloads)) throw new Error("At least one payload must be passed."); | ||
|
||
const now = dayjs.utc().toDate(); | ||
const rows = await this.builder(tx) | ||
.insert(payloads.map(payload => ({ | ||
user_id: payload.userId, | ||
start: dayjs.utc(payload.start).toDate(), | ||
end: dayjs.utc(payload.end).toDate(), | ||
created_at: now, | ||
updated_at: now, | ||
}))) | ||
.returning("*"); | ||
|
||
return rows.map(row => this.from(row)); | ||
} | ||
|
||
async delete( | ||
ids: number[], | ||
tx?: Knex.Transaction | ||
): Promise<IAvailabilitySlot.Self[]> { | ||
if (isEmpty(ids)) throw new Error("At least one id must be passed."); | ||
|
||
const rows = await this.builder(tx) | ||
.whereIn(this.column("id"), ids) | ||
.delete() | ||
.returning("*"); | ||
|
||
return rows.map(row => this.from(row)); | ||
} | ||
|
||
async update( | ||
id: number, | ||
payload: IAvailabilitySlot.UpdatePayload, | ||
tx?: Knex.Transaction | ||
): Promise<IAvailabilitySlot.Self> { | ||
const rows = await this.builder(tx) | ||
.update({ | ||
start: payload.start ? dayjs.utc(payload.start).toDate() : undefined, | ||
end: payload.end ? dayjs.utc(payload.end).toDate() : undefined, | ||
updated_at: dayjs.utc().toDate(), | ||
}) | ||
.where("id", id) | ||
.returning("*"); | ||
|
||
const row = first(rows); | ||
if (!row) throw new Error("Slot not found; should never happen."); | ||
return this.from(row); | ||
} | ||
|
||
async find({ | ||
tx, | ||
users, | ||
after, | ||
before, | ||
}: WithOptionalTx<SearchFilter> | ||
): Promise<IAvailabilitySlot.Self[]> { | ||
const baseBuilder = this.applySearchFilter(this.builder(tx), { | ||
users, | ||
after, | ||
before, | ||
}); | ||
const rows = await baseBuilder.clone().select(); | ||
return rows.map(row => this.from(row)); | ||
} | ||
|
||
applySearchFilter<R extends object, T>( | ||
builder: Knex.QueryBuilder<R, T>, | ||
{ | ||
users, | ||
after, | ||
before, | ||
}: SearchFilter | ||
): Knex.QueryBuilder<R, T> { | ||
if (users && !isEmpty(users)) | ||
builder.whereIn(this.column("user_id"), users); | ||
|
||
if (after) | ||
builder.where(this.column("start"), ">=", dayjs.utc(after).toDate()); | ||
if (before) | ||
builder.where(this.column("end"), "<=", dayjs.utc(before).toDate()); | ||
|
||
return builder; | ||
} | ||
|
||
from(row: IAvailabilitySlot.Row): IAvailabilitySlot.Self { | ||
return { | ||
id: row.id, | ||
userId: row.user_id, | ||
start: row.start.toISOString(), | ||
end: row.end.toISOString(), | ||
createAt: row.created_at.toISOString(), | ||
updatedAt: row.updated_at.toISOString(), | ||
}; | ||
} | ||
|
||
builder(tx?: Knex.Transaction) { | ||
return tx ? tx<IAvailabilitySlot.Row>(this.table) : knex<IAvailabilitySlot.Row>(this.table); | ||
} | ||
|
||
column(value: keyof IAvailabilitySlot.Row) { | ||
return column(value, this.table); | ||
} | ||
} | ||
|
||
export const availabilitySlots = new AvailabilitySlots(); |
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,142 @@ | ||
import { availabilitySlots } from "@/availabilitySlot"; | ||
import fixtures from "@fixtures/db"; | ||
import { dayjs, nameof, safe } from "@litespace/sol"; | ||
import { expect } from "chai"; | ||
import { first } from "lodash"; | ||
|
||
describe("AvailabilitySlots", () => { | ||
beforeEach(async () => { | ||
return await fixtures.flush(); | ||
}); | ||
|
||
describe(nameof(availabilitySlots.create), () => { | ||
it("should create a list of new AvailabilitySlot rows in the database", async () => { | ||
const user = await fixtures.user({}); | ||
const res = await availabilitySlots.create([ | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().toISOString(), | ||
end: dayjs.utc().add(1, "hour").toISOString() | ||
}, | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().add(2, "hour").toISOString(), | ||
end: dayjs.utc().add(4, "hour").toISOString() | ||
}, | ||
]); | ||
expect(res).to.have.length(2); | ||
}); | ||
it("should throw an error if the payloads list is empty", async () => { | ||
const res = await safe(async () => availabilitySlots.create([])); | ||
expect(res).to.be.instanceOf(Error); | ||
}); | ||
}); | ||
|
||
describe(nameof(availabilitySlots.find), () => { | ||
it("should retieve AvailabilitySlot rows of a list of users", async () => { | ||
const user1 = await fixtures.user({}); | ||
const user2 = await fixtures.user({}); | ||
|
||
const slots = await availabilitySlots.create([ | ||
{ | ||
userId: user1.id, | ||
start: dayjs.utc().toISOString(), | ||
end: dayjs.utc().add(1, "hour").toISOString() | ||
}, | ||
{ | ||
userId: user1.id, | ||
start: dayjs.utc().add(2, "hour").toISOString(), | ||
end: dayjs.utc().add(4, "hour").toISOString() | ||
}, | ||
{ | ||
userId: user2.id, | ||
start: dayjs.utc().add(6, "hour").toISOString(), | ||
end: dayjs.utc().add(7, "hour").toISOString() | ||
}, | ||
]); | ||
|
||
const res = await availabilitySlots.find({ users: [user1.id, user2.id] }) | ||
expect(res).to.have.length(3); | ||
expect(res).to.deep.eq(slots); | ||
}); | ||
it("should retieve AvailabilitySlot rows between two dates", async () => { | ||
const user = await fixtures.user({}); | ||
|
||
const slots = await availabilitySlots.create([ | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().toISOString(), | ||
end: dayjs.utc().add(1, "hour").toISOString() | ||
}, | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().add(2, "hour").toISOString(), | ||
end: dayjs.utc().add(4, "hour").toISOString() | ||
}, | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().add(6, "hour").toISOString(), | ||
end: dayjs.utc().add(7, "hour").toISOString() | ||
}, | ||
]); | ||
|
||
const res = await availabilitySlots.find({ | ||
after: slots[0].start, | ||
before: slots[1].end | ||
}); | ||
|
||
expect(res).to.have.length(2); | ||
expect(res).to.deep.eq(slots.slice(0,2)); | ||
}); | ||
}); | ||
|
||
describe(nameof(availabilitySlots.update), () => { | ||
it("should update an available AvailabilitySlot row in the database", async () => { | ||
const user = await fixtures.user({}); | ||
|
||
const slots = await availabilitySlots.create([ | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().toISOString(), | ||
end: dayjs.utc().add(1, "hour").toISOString() | ||
} | ||
]); | ||
|
||
const updated = await availabilitySlots.update(slots[0].id, { | ||
start: dayjs.utc().add(2, "hour").toISOString(), | ||
end: dayjs.utc().add(3, "hour").toISOString() | ||
}); | ||
|
||
const found = first(await availabilitySlots.find({ users: [user.id] })); | ||
expect(found).to.deep.eq(updated); | ||
}); | ||
}); | ||
|
||
describe(nameof(availabilitySlots.delete), () => { | ||
it("should delete a list of available AvailabilitySlot rows from the database", async () => { | ||
const user = await fixtures.user({}); | ||
|
||
const created = await availabilitySlots.create([ | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().toISOString(), | ||
end: dayjs.utc().add(1, "hour").toISOString() | ||
}, | ||
{ | ||
userId: user.id, | ||
start: dayjs.utc().add(2, "hour").toISOString(), | ||
end: dayjs.utc().add(4, "hour").toISOString() | ||
}, | ||
]); | ||
|
||
await availabilitySlots.delete(created.map(slot => slot.id)); | ||
|
||
const res = await availabilitySlots.find({ users: [user.id] }); | ||
expect(res).to.have.length(0); | ||
}); | ||
it("should throw an error if the ids list is empty", async () => { | ||
const res = await safe(async () => availabilitySlots.delete([])); | ||
expect(res).to.be.instanceOf(Error); | ||
}); | ||
}); | ||
}); |
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,29 @@ | ||
export type Self = { | ||
id: number; | ||
userId: number; | ||
start: string; | ||
end: string; | ||
createAt: string; | ||
updatedAt: string; | ||
}; | ||
|
||
export type Row = { | ||
id: number; | ||
user_id: number; | ||
start: Date; | ||
end: Date; | ||
created_at: Date; | ||
updated_at: Date; | ||
}; | ||
|
||
export type CreatePayload = { | ||
userId: number; | ||
start: string; | ||
end: string; | ||
}; | ||
|
||
export type UpdatePayload = { | ||
start: string; | ||
end: string; | ||
}; | ||
|
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
Oops, something went wrong.