Skip to content
Merged
Show file tree
Hide file tree
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
97 changes: 50 additions & 47 deletions packages/core/src/__mocks__/v1/events/events.22mar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ObjectId, WithId } from "mongodb";
import { Priorities } from "../../../constants/core.constants";
import { Schema_Event } from "../../../types/event.types";
import { ObjectId } from "bson";
import { Priorities } from "@core/constants/core.constants";
import { Schema_Event, WithMongoId } from "@core/types/event.types";

const allDayEventsThatShouldMatch: Array<WithId<Omit<Schema_Event, "_id">>> =
const allDayEventsThatShouldMatch: Array<
WithMongoId<Omit<Schema_Event, "_id">>
> =
// ordered by start date
[
{
Expand Down Expand Up @@ -43,49 +45,50 @@ const allDayEventsThatShouldMatch: Array<WithId<Omit<Schema_Event, "_id">>> =
},
];

const allDayEventsThatShouldNotMatch: Array<WithId<Omit<Schema_Event, "_id">>> =
[
{
_id: new ObjectId(),
user: "user1",
title: "Feb 28 - Mar 5",
isAllDay: true,
isSomeday: false,
startDate: "2022-02-28",
endDate: "2022-03-05",
priority: Priorities.WORK,
},
{
_id: new ObjectId(),
user: "user1",
title: "Mar 5",
isAllDay: true,
isSomeday: false,
startDate: "2022-03-05",
endDate: "2022-03-06",
priority: Priorities.WORK,
},
{
_id: new ObjectId(),
user: "user1",
title: "Mar 13",
isAllDay: true,
isSomeday: false,
startDate: "2022-03-13",
endDate: "2022-03-14",
priority: Priorities.WORK,
},
{
_id: new ObjectId(),
user: "user1",
title: "Mar 13 - 16",
isAllDay: true,
isSomeday: false,
startDate: "2022-03-13",
endDate: "2022-03-17",
priority: Priorities.WORK,
},
];
const allDayEventsThatShouldNotMatch: Array<
WithMongoId<Omit<Schema_Event, "_id">>
> = [
{
_id: new ObjectId(),
user: "user1",
title: "Feb 28 - Mar 5",
isAllDay: true,
isSomeday: false,
startDate: "2022-02-28",
endDate: "2022-03-05",
priority: Priorities.WORK,
},
{
_id: new ObjectId(),
user: "user1",
title: "Mar 5",
isAllDay: true,
isSomeday: false,
startDate: "2022-03-05",
endDate: "2022-03-06",
priority: Priorities.WORK,
},
{
_id: new ObjectId(),
user: "user1",
title: "Mar 13",
isAllDay: true,
isSomeday: false,
startDate: "2022-03-13",
endDate: "2022-03-14",
priority: Priorities.WORK,
},
{
_id: new ObjectId(),
user: "user1",
title: "Mar 13 - 16",
isAllDay: true,
isSomeday: false,
startDate: "2022-03-13",
endDate: "2022-03-17",
priority: Priorities.WORK,
},
];

export const mockEventSetMar22: Array<Omit<Schema_Event, "_id">> = [
...allDayEventsThatShouldMatch,
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/mappers/map.event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-namespace */
import mergeWith from "lodash.mergewith";
import { WithId } from "mongodb";
import { Origin, Priorities } from "@core/constants/core.constants";
import { BaseError } from "@core/errors/errors.base";
import {
Expand All @@ -11,6 +10,7 @@ import {
Schema_Event_Recur_Instance,
Schema_Event_Regular,
WithCompassId,
WithMongoId,
WithoutCompassId,
} from "@core/types/event.types";
import { WithGcalId, gSchema$Event } from "@core/types/gcal";
Expand All @@ -37,9 +37,9 @@ export namespace MapEvent {
};

export const removeProviderData = (
event: WithId<Omit<Schema_Event, "_id">> | Schema_Event,
event: WithMongoId<Omit<Schema_Event, "_id">> | Schema_Event,
): Omit<
WithId<Omit<Schema_Event, "_id">> | Schema_Event,
WithMongoId<Omit<Schema_Event, "_id">> | Schema_Event,
"gEventId" | "gRecurringEventId"
> => {
const {
Expand All @@ -57,7 +57,7 @@ export namespace MapEvent {
};

export const removeIdentifyingData = (
event: WithId<Omit<Schema_Event, "_id">> | Schema_Event,
event: WithMongoId<Omit<Schema_Event, "_id">> | Schema_Event,
): Omit<
Schema_Event,
| "_id"
Expand Down Expand Up @@ -128,7 +128,7 @@ export namespace MapEvent {

export const toGcalSingleProviderData = (
base:
| WithId<Omit<Schema_Event_Recur_Base | Schema_Event_Regular, "_id">>
| WithMongoId<Omit<Schema_Event_Recur_Base | Schema_Event_Regular, "_id">>
| WithCompassId<
Omit<Schema_Event_Recur_Base | Schema_Event_Regular, "_id">
>,
Expand All @@ -140,11 +140,11 @@ export namespace MapEvent {

export const toProviderData = (
event:
| WithId<Omit<Schema_Event, "_id" | "recurrence">>
| WithMongoId<Omit<Schema_Event, "_id" | "recurrence">>
| WithCompassId<Omit<Schema_Event, "_id" | "recurrence">>,
provider?: CalendarProvider,
base?:
| WithId<Omit<Schema_Event_Recur_Base, "_id">>
| WithMongoId<Omit<Schema_Event_Recur_Base, "_id">>
| WithCompassId<Omit<Schema_Event_Recur_Base, "_id">>,
) => {
const isCInstance = isInstance(event);
Expand All @@ -153,7 +153,7 @@ export namespace MapEvent {
case CalendarProvider.GOOGLE: {
return isCInstance
? MapEvent.toGcalInstanceProviderData(
event as WithId<Omit<Schema_Event_Recur_Instance, "_id">>,
event as WithMongoId<Omit<Schema_Event_Recur_Instance, "_id">>,
base,
)
: MapEvent.toGcalSingleProviderData(event);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/mongo.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ObjectId } from "mongodb";
import { ObjectId } from "bson";

export type InsertedIds = { [key: number]: ObjectId };
2 changes: 1 addition & 1 deletion packages/core/src/types/sync.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyBulkWriteOperation, BulkWriteResult } from "mongodb";
import type { AnyBulkWriteOperation, BulkWriteResult } from "mongodb";
import { BaseError } from "@core/errors/errors.base";

export interface Params_Sync_Gcal extends Payload_Sync_Notif {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/util/event/compass.event.rrule.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjectId, type WithId } from "mongodb";
import { ObjectId } from "bson";
import { RRule } from "rrule";
import { faker } from "@faker-js/faker";
import { recurring } from "@core/__mocks__/v1/events/gcal/gcal.recurring";
Expand All @@ -7,6 +7,7 @@ import { gEventToCompassEvent } from "@core/mappers/map.event";
import {
CalendarProvider,
type Schema_Event_Recur_Base,
WithMongoId,
} from "@core/types/event.types";
import dayjs from "@core/util/date/dayjs";
import { CompassEventRRule } from "@core/util/event/compass.event.rrule";
Expand Down Expand Up @@ -390,7 +391,7 @@ describe("CompassEventRRule: ", () => {
return event;
});

const baseEvent = compassEvents.find(isBase) as WithId<
const baseEvent = compassEvents.find(isBase) as WithMongoId<
Omit<Schema_Event_Recur_Base, "_id">
>;

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/util/event/compass.event.rrule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ObjectId } from "bson";
import type { WithId } from "mongodb";
import type { Options, RRuleStrOptions } from "rrule";
import { RRule, rrulestr } from "rrule";
import type { ParsedOptions } from "rrule/dist/esm/types";
Expand All @@ -9,6 +8,7 @@ import type {
CalendarProvider,
Schema_Event_Recur_Base,
Schema_Event_Recur_Instance,
WithMongoId,
} from "@core/types/event.types";
import dayjs from "@core/util/date/dayjs";
import {
Expand All @@ -18,13 +18,13 @@ import {
} from "@core/util/event/event.util";

export class CompassEventRRule extends RRule {
#event: WithId<Omit<Schema_Event_Recur_Base, "_id">>;
#event: WithMongoId<Omit<Schema_Event_Recur_Base, "_id">>;
#dateFormat: string;
#durationMs!: number;

constructor(
event: Pick<
WithId<Omit<Schema_Event_Recur_Base, "_id">>,
WithMongoId<Omit<Schema_Event_Recur_Base, "_id">>,
"startDate" | "endDate" | "_id" | "recurrence"
>,
options: Partial<Options> = {},
Expand All @@ -41,7 +41,7 @@ export class CompassEventRRule extends RRule {
}

static #initOptions(
event: WithId<Omit<Schema_Event_Recur_Base, "_id">>,
event: WithMongoId<Omit<Schema_Event_Recur_Base, "_id">>,
_options: Partial<Options> = {},
): Partial<Options> {
const startDate = parseCompassEventDate(event.startDate!);
Expand Down Expand Up @@ -111,7 +111,7 @@ export class CompassEventRRule extends RRule {

base(
provider?: CalendarProvider,
): WithId<Omit<Schema_Event_Recur_Base, "_id">> {
): WithMongoId<Omit<Schema_Event_Recur_Base, "_id">> {
const _id = this.#event._id ?? new ObjectId();
const recurrence = { rule: this.toRecurrence() };
const event = { ...this.#event, _id, recurrence };
Expand All @@ -129,7 +129,7 @@ export class CompassEventRRule extends RRule {
*/
instances(
provider?: CalendarProvider,
): WithId<Omit<Schema_Event_Recur_Instance, "_id">>[] {
): WithMongoId<Omit<Schema_Event_Recur_Instance, "_id">>[] {
const base = this.base();
const baseData = MapEvent.removeIdentifyingData(base);
const baseEventId = base._id.toString();
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/util/test/ccal.event.factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjectId } from "mongodb";
import { ObjectId } from "bson";
import { faker } from "@faker-js/faker";
import { Origin, Priorities } from "@core/constants/core.constants";
import {
Expand All @@ -8,7 +8,7 @@ import {
WithCompassId,
} from "@core/types/event.types";
import dayjs from "@core/util/date/dayjs";
import { isAllDay, parseCompassEventDate } from "../event/event.util";
import { isAllDay, parseCompassEventDate } from "@core/util/event/event.util";

export const createMockStandaloneEvent = (
overrides: Partial<Schema_Event> = {},
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/common/types/web.event.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const WebEventRecurrence = z.union([
]);

const WebCoreEventSchema = CompassCoreEventSchema.extend({
_id: z.union([idSchema, optimisticIdSchema]),
_id: z.union([idSchema, optimisticIdSchema]).optional(),
recurrence: WebEventRecurrence,
order: z.number().optional(),
});
Expand Down
37 changes: 36 additions & 1 deletion packages/web/src/common/utils/event.util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { isEventInRange } from "./event.util";
import { createMockStandaloneEvent } from "@core/util/test/ccal.event.factory";
import {
Schema_GridEvent,
Schema_WebEvent,
} from "@web/common/types/web.event.types";
import { isEventInRange } from "@web/common/utils/event.util";
import { _assembleGridEvent } from "@web/ducks/events/sagas/saga.util";

describe("isEventInRange", () => {
it("returns true if event is in range", () => {
Expand All @@ -9,6 +15,7 @@ describe("isEventInRange", () => {
};
expect(isEventInRange(event, dates)).toBe(true);
});

it("returns false if event is not in range", () => {
const event = { start: "2022-03-15", end: "2022-03-15" };
const dates = {
Expand All @@ -18,3 +25,31 @@ describe("isEventInRange", () => {
expect(isEventInRange(event, dates)).toBe(false);
});
});

describe("_assembleGridEvent", () => {
it("should successfully convert Someday event to Grid event by adding position field", () => {
// Create a mock Someday event (without position field)
const somedayEvent = createMockStandaloneEvent({
isSomeday: true,
}) as Schema_WebEvent;

const generator = _assembleGridEvent(somedayEvent);

// First, it calls getEventById
const getEventStep = generator.next();
expect(getEventStep.done).toBe(false);

// Mock returning the Someday event
const validateStep = generator.next({ ...somedayEvent, isSomeday: false });

// This should now succeed because the fix adds the required position field
expect(validateStep.done).toBe(true);
const result = validateStep.value as Schema_GridEvent;

// Verify that position field is now present
expect(result.position).toBeDefined();
expect(result.position.isOverlapping).toBe(false);
expect(result.position.widthMultiplier).toBe(1);
expect(result.position.horizontalOrder).toBe(1);
});
});
17 changes: 7 additions & 10 deletions packages/web/src/common/utils/event.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import {
Schema_OptimisticEvent,
Schema_WebEvent,
} from "@web/common/types/web.event.types";
import { validateGridEvent } from "@web/common/validators/grid.event.validator";
import { validateSomedayEvent } from "@web/common/validators/someday.event.validator";
import { validateGridEvent } from "../validators/grid.event.validator";

const gridEventDefaultPosition = {
export const gridEventDefaultPosition = {
isOverlapping: false,
widthMultiplier: 1,
horizontalOrder: 1,
Expand Down Expand Up @@ -87,7 +87,7 @@ export const assembleDefaultEvent = async (
}
};

export const assembleGridEvent = (event: Schema_Event): Schema_GridEvent => {
export const assembleGridEvent = (event: Schema_WebEvent): Schema_GridEvent => {
const gridEvent: Schema_GridEvent = {
...event,
position: gridEventDefaultPosition,
Expand All @@ -97,7 +97,7 @@ export const assembleGridEvent = (event: Schema_Event): Schema_GridEvent => {
origin: event.origin ?? Origin.COMPASS,
priority: event.priority ?? Priorities.UNASSIGNED,
user: event.user!,
recurrence: event.recurrence as Schema_Event_Recur_Base["recurrence"],
recurrence: event.recurrence,
};

return gridEvent;
Expand Down Expand Up @@ -203,13 +203,10 @@ export const prepEvtAfterDraftDrop = (
return event;
};

export const prepEvtBeforeSubmit = (
draft: Schema_Event | Schema_GridEvent,
userId: string,
) => {
const _event: Omit<Schema_Event | Schema_GridEvent, "recurrence"> = {
export const prepEvtBeforeSubmit = (draft: Schema_WebEvent, userId: string) => {
const _event = {
...draft,
origin: Origin.COMPASS,
origin: draft.origin ?? Origin.COMPASS,
user: userId,
};

Expand Down
Loading