Remove Scrim.getStartTime and read the booked start time from post.startsAt, which the repository mapper already resolves

This commit is contained in:
Kalle
2026-11-11 16:57:53 +02:00
parent a969eb99e0
commit 1e670efee5
9 changed files with 59 additions and 53 deletions

View File

@@ -314,6 +314,56 @@ describe("findUserScrims", () => {
});
});
describe("findById", () => {
const WINDOW_OPENS_AT = dbTs(BOOKED_AT) - 3600;
const WINDOW_CLOSES_AT = dbTs(BOOKED_AT) + 3600;
beforeEach(async () => {
await users.create(2);
});
test.each([
{
why: "the accepted request's chosen start",
request: { isAccepted: true, startsAt: dbTs(BOOKED_AT) },
expected: {
startsAt: dbTs(BOOKED_AT),
rangeEndsAt: null,
},
},
{
why: "the window start when the accepted request chose none",
request: { isAccepted: true, startsAt: undefined },
expected: { startsAt: WINDOW_OPENS_AT, rangeEndsAt: null },
},
{
why: "the window while no request is accepted",
request: { isAccepted: false, startsAt: dbTs(BOOKED_AT) },
expected: {
startsAt: WINDOW_OPENS_AT,
rangeEndsAt: WINDOW_CLOSES_AT,
},
},
])("starts at $why", async ({ request, expected }) => {
const { id } = await ScrimPostFactory.create(
{
startsAt: WINDOW_OPENS_AT,
rangeEndsAt: WINDOW_CLOSES_AT,
users: [{ userId: users.id(1), isOwner: 1 }],
},
{
requests: [
{ ...request, users: [{ userId: users.id(2), isOwner: 1 }] },
],
},
);
const post = await ScrimPostRepository.findById(id);
expect(post).toMatchObject(expected);
});
});
describe("findAcceptedScrimsBetweenTwoTimestamps", () => {
beforeEach(async () => {
await users.create(2);

View File

@@ -432,13 +432,14 @@ const mapDBRowToScrimPost = (
canceled,
};
if (!Scrim.isAccepted(result)) {
const acceptedRequest = result.requests.find((r) => r.isAccepted);
if (!acceptedRequest) {
return result;
}
return {
...result,
startsAt: Scrim.getStartTime(result),
startsAt: acceptedRequest.startsAt ?? result.startsAt,
rangeEndsAt: null,
};
};

View File

@@ -37,7 +37,7 @@ export const action = defineAction(
errorToastIfFalsy(Scrim.isAccepted(post), "Scrim is not accepted");
errorToastIfFalsy(!post.canceled, "Scrim is already canceled");
if (databaseTimestampToDate(Scrim.getStartTime(post)) < new Date()) {
if (databaseTimestampToDate(post.startsAt) < new Date()) {
errorToast(
"Cannot cancel a scrim that was already scheduled to start",
);
@@ -226,7 +226,7 @@ async function loadMapByMapContext({
if (
Scrim.isTrackingLocked({
startTime: Scrim.getStartTime(post),
startTime: post.startsAt,
maps,
mapLists,
})

View File

@@ -190,9 +190,7 @@ export const action = defineAction(
);
try {
const bookedAt = databaseTimestampToDate(
Scrim.getStartTime(fullPost),
);
const bookedAt = databaseTimestampToDate(fullPost.startsAt);
const startTime = dateToDatabaseTimestamp(
sub(bookedAt, { hours: SCRIM.AUTO_CANCEL_WINDOW_HOURS }),
);

View File

@@ -76,10 +76,7 @@ export function ScrimMatchBanner() {
function ScrimMatchBannerTopRow() {
const data = useLoaderData<typeof loader>();
const acceptedRequest = data.post.requests.find((r) => r.isAccepted);
const scheduledAt = databaseTimestampToDate(
acceptedRequest?.startsAt ?? data.post.startsAt,
);
const scheduledAt = databaseTimestampToDate(data.post.startsAt);
return (
<MatchBannerTopRow>

View File

@@ -6,7 +6,6 @@ import {
applyFilters,
defaultFilters,
filtersAreDefault,
getStartTime,
isAccepted,
isParticipating,
isTrackingLocked,
@@ -1024,40 +1023,6 @@ describe("Scrim.resolvePoolCode", () => {
});
});
describe("Scrim.getStartTime", () => {
const POST_STARTS_AT = 1_000;
const REQUEST_STARTS_AT = 2_000;
const postWithRequest = (request: {
isAccepted: boolean;
startsAt: number | null;
}) =>
({
...createPost([{ id: 1 }], [{ ...request, users: [{ id: 2 }] }]),
startsAt: POST_STARTS_AT,
}) as ScrimPost;
test.each([
{
why: "the accepted request's chosen start",
request: { isAccepted: true, startsAt: REQUEST_STARTS_AT },
expected: REQUEST_STARTS_AT,
},
{
why: "the post's start when the accepted request chose none",
request: { isAccepted: true, startsAt: null },
expected: POST_STARTS_AT,
},
{
why: "the post's start while no request is accepted",
request: { isAccepted: false, startsAt: REQUEST_STARTS_AT },
expected: POST_STARTS_AT,
},
])("$why", ({ request, expected }) => {
expect(getStartTime(postWithRequest(request))).toBe(expected);
});
});
describe("Scrim.defaultFilters", () => {
test("sets no filter", () => {
expect(defaultFilters()).toEqual({

View File

@@ -65,12 +65,6 @@ export function participantIdsListFromAccepted(post: ScrimPost) {
.concat(acceptedRequest?.users.map((u) => u.id) ?? []);
}
/** The scrim's actual start: the accepted request's time when the post has a range (rangeEndsAt set), else the post's start. */
export function getStartTime(post: ScrimPost): number {
const acceptedRequest = post.requests.find((r) => r.isAccepted);
return acceptedRequest?.startsAt ?? post.startsAt;
}
/** Display name of a scrim side: the team name, else "{ownerUsername}'s pickup". */
export function sideDisplayName(side: {
team: { name: string } | null;

View File

@@ -87,7 +87,7 @@ async function resolveMapByMap({
const currentMap = maps.find((m) => m.reportedAt === null) ?? null;
const viewerSide = Scrim.sideOfUser(post, user.id);
const locked = Scrim.isTrackingLocked({
startTime: Scrim.getStartTime(post),
startTime: post.startsAt,
maps,
mapLists,
});

View File

@@ -7,6 +7,7 @@ export type ScrimSide = "ALPHA" | "BRAVO";
export interface ScrimPost {
id: number;
/** When the scrim starts. For an accepted post this is the booked time, not the window start. */
startsAt: number;
rangeEndsAt: number | null;
createdAt: number;