Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 84 additions & 37 deletions tests/integration/labor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient, createLocation, newTestUuid } from "./helpers";
import { createClient } from "./helpers";
import { SquareClient } from "../../src";

function formatDateString(date: Date): string {
Expand All @@ -13,51 +13,74 @@ describe("Labor API", () => {
let shiftId: string;

beforeAll(async () => {
// Get first location
locationId = await createLocation(client);

// Create team member for testing
const teamResponse = await client.teamMembers.create({
idempotencyKey: newTestUuid(),
teamMember: {
givenName: "Sherlock",
familyName: "Holmes",
// Get first available location
const locations = await client.locations.list();
if (!locations.locations?.length) {
throw new Error("No locations available for testing");
}
locationId = locations.locations[0].id!;

// Get first available team member at this location
const teamMembers = await client.teamMembers.search({
query: {
filter: {
locationIds: [locationId],
status: "ACTIVE",
},
},
});
memberId = teamResponse.teamMember!.id!;
if (!teamMembers.teamMembers?.length) {
throw new Error(`No team members available at location ${locationId}`);
}
memberId = teamMembers.teamMembers[0].id!;

// Create break type for testing
const breakResponse = await client.labor.breakTypes.create({
idempotencyKey: newTestUuid(),
idempotencyKey: crypto.randomUUID(),
breakType: {
locationId: locationId,
expectedDuration: "PT0H30M0S", // 30 min duration in RFC 3339 format
breakName: `Lunch_${newTestUuid()}`,
breakName: `Lunch_${crypto.randomUUID()}`,
isPaid: true,
},
});
breakId = breakResponse.breakType!.id!;
if (!breakResponse.breakType?.id) {
throw new Error("Failed to create break type");
}
breakId = breakResponse.breakType.id;

// Create shift for testing
const shiftResponse = await client.labor.shifts.create({
idempotencyKey: newTestUuid(),
idempotencyKey: crypto.randomUUID(),
shift: {
startAt: formatDateString(new Date()),
locationId: locationId,
teamMemberId: memberId,
},
});
shiftId = shiftResponse.shift!.id!;
if (!shiftResponse.shift?.id) {
throw new Error("Failed to create shift");
}
shiftId = shiftResponse.shift.id;
});

afterAll(async () => {
// Clean up resources
await client.labor.shifts.delete({
id: shiftId,
});
await client.labor.breakTypes.delete({
id: breakId,
});
try {
await client.labor.shifts.delete({
id: shiftId,
});
} catch (e) {
// Test may have already deleted the shift
}

try {
await client.labor.breakTypes.delete({
id: breakId,
});
} catch (e) {
// Test may have already deleted the break
}
});

it("should list break types", async () => {
Expand All @@ -82,7 +105,7 @@ describe("Labor API", () => {
breakType: {
locationId: locationId,
expectedDuration: "PT1H0M0S", // 1 hour duration
breakName: `Lunch_${newTestUuid()}`,
breakName: `Lunch_${crypto.randomUUID()}`,
isPaid: true,
},
});
Expand Down Expand Up @@ -134,39 +157,63 @@ describe("Labor API", () => {
});

it("should delete shift", async () => {
// create team member
const teamMemberResponse = await client.teamMembers.create({
idempotencyKey: newTestUuid(),
teamMember: {
givenName: "Sherlock",
familyName: "Holmes",
// First search for existing shifts for this team member
const existingShifts = await client.labor.shifts.search({
query: {
filter: {
teamMemberIds: [memberId],
},
},
limit: 100,
});

// create shift
// Delete any existing shifts
if (existingShifts.shifts) {
for (const shift of existingShifts.shifts) {
if (shift.id) {
await client.labor.shifts.delete({
id: shift.id,
});
}
}
}

// Start the shift 10 seconds from now and end it 20 seconds from now
const startTime = new Date(Date.now() + 10000);
const endTime = new Date(startTime.getTime() + 10000);

// Create shift
const shiftResponse = await client.labor.shifts.create({
idempotencyKey: newTestUuid(),
idempotencyKey: crypto.randomUUID(),
shift: {
startAt: formatDateString(new Date()),
locationId: locationId,
teamMemberId: teamMemberResponse.teamMember?.id!,
startAt: formatDateString(startTime),
teamMemberId: memberId,
endAt: formatDateString(endTime),
},
});

if (!shiftResponse.shift?.id) {
throw new Error("Failed to create shift");
}

// Add a small delay to ensure the shift is fully created
await new Promise(resolve => setTimeout(resolve, 1000));

const response = await client.labor.shifts.delete({
id: shiftResponse.shift?.id!,
id: shiftResponse.shift.id,
});
expect(response).toBeDefined();
});

it("should delete break type", async () => {
// create break type
const breakResponse = await client.labor.breakTypes.create({
idempotencyKey: newTestUuid(),
idempotencyKey: crypto.randomUUID(),
breakType: {
locationId: locationId,
expectedDuration: "PT0H30M0S",
breakName: `Lunch_${newTestUuid()}`,
breakName: `Lunch_${crypto.randomUUID()}`,
isPaid: true,
},
});
Expand All @@ -183,4 +230,4 @@ describe("Labor API", () => {
expect(response.data).toBeDefined();
expect(response.data?.length).toBeGreaterThan(0);
});
});
});