Skip to content

Commit

Permalink
fix EdgeGroup actions sets incorrect ent type in DB (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Dec 6, 2023
1 parent 253e195 commit 325c40a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 33 deletions.
4 changes: 4 additions & 0 deletions docker_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Changelog for the npm version are [here](/CHANGELOG.md).

## [Unreleased]

### Fixed

- fix EdgeGroup actions sets incorrect ent type in DB (#1719)

## [0.1.14] - 2023-11-06

### Fixed
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 75 additions & 26 deletions examples/ent-rsvp/backend/src/ent/tests/event_activity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
GuestToAttendingEventsQuery,
AddressToLocatedAtQuery,
} from "src/ent";
import { IDViewer } from "@snowtop/ent";
import { AssocEdge, EdgeQuery, IDViewer } from "@snowtop/ent";
import CreateEventActivityAction from "../event_activity/actions/create_event_activity_action";
import CreateGuestGroupAction from "../guest_group/actions/create_guest_group_action";
import EventActivityAddInviteAction from "../event_activity/actions/event_activity_add_invite_action";
Expand Down Expand Up @@ -248,8 +248,10 @@ describe("rsvps", () => {
async function doRsvpForSelf(
input: EventActivityRsvpStatusInput,
output: EventActivityRsvpStatus,
activityCount: (activity: EventActivity) => Promise<number>,
guestCount: (guest: Guest) => Promise<number>,
activityEdge: (
activity: EventActivity,
) => EdgeQuery<EventActivity, Guest, AssocEdge>,
guestEdge: (guest: Guest) => EdgeQuery<Guest, EventActivity, AssocEdge>,
options?: {
// must provide both eventActivity and
eventActivity?: EventActivity;
Expand All @@ -273,13 +275,20 @@ describe("rsvps", () => {
} else {
[activity, guests] = await createAndInvitePlusGuests(0);
}
const activityCount = (activity: EventActivity) =>
activityEdge(activity).queryCount();
const guestCount = (guest: Guest) => guestEdge(guest).queryCount();
const activityEdges = (activity: EventActivity) =>
activityEdge(activity).queryEdges();

expect(guests.length).toBe(2);
let guest = guests[0];
let count = await activityCount(activity);
expect(count).toBe(0);
count = await guestCount(guest);
expect(count).toBe(0);
let edges = await activityEdges(activity);
expect(edges.length).toBe(0);

const vc = new IDViewer(guest.id);
const activity2 = await EditEventActivityRsvpStatusAction.saveXFromID(
Expand All @@ -294,13 +303,28 @@ describe("rsvps", () => {

count = await activityCount(activity2);
expect(count).toBe(1);
edges = await activityEdges(activity);
expect(edges.length).toBe(1);
expect(edges[0]).toMatchObject({
id1: activity.id,
id2: guest.id,
id1Type: activity.nodeType,
id2Type: guest.nodeType,
});

// reload guest
guest = await Guest.loadX(vc, guest.id);

count = await guestCount(guest);
expect(count).toBe(1);

edges = await guestEdge(guest).queryEdges();
expect(edges.length).toBe(1);
expect(edges[0]).toMatchObject({
id1: guest.id,
id2: activity.id,
id1Type: guest.nodeType,
id2Type: activity.nodeType,
});
// rsvp status is as expected
const rsvpStatus = await activity2.rsvpStatusFor(guest);
expect(rsvpStatus).toBe(output);
Expand All @@ -311,10 +335,17 @@ describe("rsvps", () => {
async function doRsvpForOther(
input: EventActivityRsvpStatusInput,
output: EventActivityRsvpStatus,
activityCount: (activity: EventActivity) => Promise<number>,
guestCount: (guest: Guest) => Promise<number>,
activityEdge: (
activity: EventActivity,
) => EdgeQuery<EventActivity, Guest, AssocEdge>,
guestEdge: (guest: Guest) => EdgeQuery<Guest, EventActivity, AssocEdge>,
) {
const [activity, guests] = await createAndInvitePlusGuests(0);
const activityCount = (activity: EventActivity) =>
activityEdge(activity).queryCount();
const guestCount = (guest: Guest) => guestEdge(guest).queryCount();
const activityEdges = (activity: EventActivity) =>
activityEdge(activity).queryEdges();

expect(guests.length).toBe(2);
let guest = guests[0];
Expand All @@ -324,6 +355,8 @@ describe("rsvps", () => {
const vc = new IDViewer(guests[1].id);
count = await guestCount(guest);
expect(count).toBe(0);
let edges = await activityEdges(activity);
expect(edges.length).toBe(0);

const activity2 = await EditEventActivityRsvpStatusAction.saveXFromID(
vc,
Expand All @@ -336,12 +369,28 @@ describe("rsvps", () => {

count = await activityCount(activity2);
expect(count).toBe(1);
edges = await activityEdges(activity);
expect(edges.length).toBe(1);
expect(edges[0]).toMatchObject({
id1: activity.id,
id2: guest.id,
id1Type: activity.nodeType,
id2Type: guest.nodeType,
});

// reload guest
guest = await Guest.loadX(vc, guest.id);

count = await guestCount(guest);
expect(count).toBe(1);
edges = await guestEdge(guest).queryEdges();
expect(edges.length).toBe(1);
expect(edges[0]).toMatchObject({
id1: guest.id,
id2: activity.id,
id1Type: guest.nodeType,
id2Type: activity.nodeType,
});

// self rsvp is still can rsvp since didn't rsvp for self
const rsvpStatus = await activity2.rsvpStatusFor(guests[1]);
Expand All @@ -355,17 +404,17 @@ describe("rsvps", () => {
await doRsvpForSelf(
EventActivityRsvpStatusInput.Attending,
EventActivityRsvpStatus.Attending,
(activity: EventActivity) => activity.queryAttending().queryCount(),
(guest: Guest) => guest.queryGuestToAttendingEvents().queryCount(),
(activity: EventActivity) => activity.queryAttending(),
(guest: Guest) => guest.queryGuestToAttendingEvents(),
);
});

test("rsvp declined for self", async () => {
await doRsvpForSelf(
EventActivityRsvpStatusInput.Declined,
EventActivityRsvpStatus.Declined,
(activity: EventActivity) => activity.queryDeclined().queryCount(),
(guest: Guest) => guest.queryGuestToDeclinedEvents().queryCount(),
(activity: EventActivity) => activity.queryDeclined(),
(guest: Guest) => guest.queryGuestToDeclinedEvents(),
);
});

Expand All @@ -374,17 +423,17 @@ describe("rsvps", () => {
let [activity, guests] = await doRsvpForSelf(
EventActivityRsvpStatusInput.Declined,
EventActivityRsvpStatus.Declined,
(activity: EventActivity) => activity.queryDeclined().queryCount(),
(guest: Guest) => guest.queryGuestToDeclinedEvents().queryCount(),
(activity: EventActivity) => activity.queryDeclined(),
(guest: Guest) => guest.queryGuestToDeclinedEvents(),
);

// switch to attending

await doRsvpForSelf(
EventActivityRsvpStatusInput.Attending,
EventActivityRsvpStatus.Attending,
(activity: EventActivity) => activity.queryAttending().queryCount(),
(guest: Guest) => guest.queryGuestToAttendingEvents().queryCount(),
(activity: EventActivity) => activity.queryAttending(),
(guest: Guest) => guest.queryGuestToAttendingEvents(),
{
eventActivity: activity,
guests,
Expand All @@ -399,8 +448,8 @@ describe("rsvps", () => {
await doRsvpForSelf(
EventActivityRsvpStatusInput.Declined,
EventActivityRsvpStatus.Declined,
(activity: EventActivity) => activity.queryDeclined().queryCount(),
(guest: Guest) => guest.queryGuestToDeclinedEvents().queryCount(),
(activity: EventActivity) => activity.queryDeclined(),
(guest: Guest) => guest.queryGuestToDeclinedEvents(),
{
eventActivity: activity,
guests,
Expand All @@ -417,8 +466,8 @@ describe("rsvps", () => {
let [activity, guests] = await doRsvpForSelf(
EventActivityRsvpStatusInput.Attending,
EventActivityRsvpStatus.Attending,
(activity: EventActivity) => activity.queryAttending().queryCount(),
(guest: Guest) => guest.queryGuestToAttendingEvents().queryCount(),
(activity: EventActivity) => activity.queryAttending(),
(guest: Guest) => guest.queryGuestToAttendingEvents(),
{
dietaryRestrictions: "shellfish",
},
Expand Down Expand Up @@ -465,8 +514,8 @@ describe("rsvps", () => {
await doRsvpForSelf(
EventActivityRsvpStatusInput.Declined,
EventActivityRsvpStatus.Declined,
(activity: EventActivity) => activity.queryDeclined().queryCount(),
(guest: Guest) => guest.queryGuestToDeclinedEvents().queryCount(),
(activity: EventActivity) => activity.queryDeclined(),
(guest: Guest) => guest.queryGuestToDeclinedEvents(),
{
eventActivity: activity,
guests: guests,
Expand All @@ -481,8 +530,8 @@ describe("rsvps", () => {
await doRsvpForSelf(
EventActivityRsvpStatusInput.Attending,
EventActivityRsvpStatus.Attending,
(activity: EventActivity) => activity.queryAttending().queryCount(),
(guest: Guest) => guest.queryGuestToAttendingEvents().queryCount(),
(activity: EventActivity) => activity.queryAttending(),
(guest: Guest) => guest.queryGuestToAttendingEvents(),
{
eventActivity: activity,
guests: guests,
Expand All @@ -506,17 +555,17 @@ describe("rsvps", () => {
await doRsvpForOther(
EventActivityRsvpStatusInput.Attending,
EventActivityRsvpStatus.Attending,
(activity: EventActivity) => activity.queryAttending().queryCount(),
(guest: Guest) => guest.queryGuestToAttendingEvents().queryCount(),
(activity: EventActivity) => activity.queryAttending(),
(guest: Guest) => guest.queryGuestToAttendingEvents(),
);
});

test("rsvp declined for other guest in group", async () => {
await doRsvpForOther(
EventActivityRsvpStatusInput.Declined,
EventActivityRsvpStatus.Declined,
(activity: EventActivity) => activity.queryDeclined().queryCount(),
(guest: Guest) => guest.queryGuestToDeclinedEvents().queryCount(),
(activity: EventActivity) => activity.queryDeclined(),
(guest: Guest) => guest.queryGuestToDeclinedEvents(),
);
});

Expand Down
6 changes: 6 additions & 0 deletions examples/ent-rsvp/backend/src/testutils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CreateGuestAction, {
} from "src/ent/guest/actions/create_guest_action";
import EventActivityAddInviteAction from "src/ent/event_activity/actions/event_activity_add_invite_action";
import { Builder } from "@snowtop/ent/action";
import { NodeType } from "src/ent/generated/types";

export async function createUser() {
const user = await CreateUserAction.create(new LoggedOutViewer(), {
Expand Down Expand Up @@ -122,6 +123,11 @@ export async function createAndInvite(): Promise<[EventActivity, GuestGroup]> {
const newCount = await reloaded.queryInvites().queryCount();
expect(newCount).toBe(1);

const edges = await reloaded.queryInvites().queryEdges();
expect(edges.length).toBe(1);
expect(edges[0].id1Type).toBe(NodeType.EventActivity);
expect(edges[0].id2Type).toBe(NodeType.GuestGroup);

return [activity, group];
}

Expand Down
2 changes: 1 addition & 1 deletion internal/tscode/action_base.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class {{$actionName}} implements {{useImport "Action"}}<{{$node}}, {{useI
this.input.{{$edgeGroup.TSGroupStatusName}},
this.{{$instance}}.id,
this.input.{{$edgeGroup.GetIDArg}},
{{useImport "NodeType"}}.{{$edgeGroup.NodeInfo.Node}},
{{useImport "NodeType"}}.{{$edgeGroup.DestNodeInfo.Node}},
this.{{$instance}}.{{$edgeGroup.GetStatusMapMethod}}(),
);
}
Expand Down
8 changes: 4 additions & 4 deletions ts/src/action/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ async function modifyEdgeSet<T extends string>(
inputEnumValue: string,
enumValue: string,
edgeType: T,
nodeType: string,
id2NodeType: string,
) {
let edge = await loadEdgeForID2({
id1: id1,
Expand All @@ -280,7 +280,7 @@ async function modifyEdgeSet<T extends string>(
// TODO: can save a write here by checking in EdgeOperation and not doing this write if nothing
// has changed.
if (inputEnumValue === enumValue) {
orchestrator.addOutboundEdge(id2, edgeType, nodeType);
orchestrator.addOutboundEdge(id2, edgeType, id2NodeType);
}
if (edge) {
if (enumValue !== inputEnumValue) {
Expand Down Expand Up @@ -308,13 +308,13 @@ export async function setEdgeTypeInGroup<T extends string>(
inputEnumValue: string,
id1: ID,
id2: ID,
nodeType: string,
id2NodeType: string,
m: Map<T, string>,
) {
let promises: Promise<void>[] = [];
for (const [k, v] of m) {
promises.push(
modifyEdgeSet(orchestrator, id1, id2, inputEnumValue, k, v, nodeType),
modifyEdgeSet(orchestrator, id1, id2, inputEnumValue, k, v, id2NodeType),
);
}
await Promise.all(promises);
Expand Down

0 comments on commit 325c40a

Please sign in to comment.