Skip to content

Commit

Permalink
Wrap up fundraising form
Browse files Browse the repository at this point in the history
  • Loading branch information
jthoward64 committed Dec 2, 2024
1 parent ef81136 commit aa8fa35
Show file tree
Hide file tree
Showing 16 changed files with 630 additions and 616 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { NonEmptyStringResolver } from "graphql-scalars";
import { LocalDateResolver, NonEmptyStringResolver } from "graphql-scalars";
import { ArgsType, Field, Float, InputType, ObjectType } from "type-graphql";

import { FilteredListQueryArgs } from "../filtering/list-query-args/FilteredListQueryArgs.js";
import { BatchType } from "../resources/DailyDepartmentNotification.js";
import { FundraisingEntryNode } from "../resources/Fundraising.js";
import { type GlobalId, GlobalIdScalar } from "../scalars/GlobalId.js";
import { AbstractGraphQLPaginatedResponse } from "./ApiResponse.js";
import type { LocalDate } from "../../utility/time/localDate.js";

@ArgsType()
export class ListFundraisingEntriesArgs extends FilteredListQueryArgs<
Expand Down Expand Up @@ -62,8 +63,8 @@ export class SetFundraisingEntryInput {
@Field(() => Float, { nullable: true })
amountOverride?: number;

@Field(() => NonEmptyStringResolver, { nullable: true })
donatedOnOverride?: string;
@Field(() => LocalDateResolver, { nullable: true })
donatedOnOverride?: LocalDate;

@Field(() => NonEmptyStringResolver, { nullable: true })
donatedToOverride?: string;
Expand Down
13 changes: 9 additions & 4 deletions packages/common/lib/api/resources/Fundraising.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export class FundraisingEntryNode extends TimestampedResource implements Node {
@Field(() => String, { nullable: true, name: "donatedToOverride" })
donatedToOverride!: string | null | undefined;

@Field(() => DateTimeISOResolver)
donatedOn!: Date;
get donatedOnDateTime(): DateTime {
@Field(() => DateTimeISOResolver, { nullable: true, name: "donatedOn" })
donatedOn!: Date | null | undefined;
get donatedOnDateTime(): DateTime | null | undefined {
return dateTimeFromSomething(this.donatedOn);
}

Expand Down Expand Up @@ -72,15 +72,20 @@ export class FundraisingEntryNode extends TimestampedResource implements Node {
public static init(init: {
id: string;
donatedByText: string | null;
donatedByOverride: string | null;
donatedToText: string | null;
donatedOn: Date;
donatedToOverride: string | null;
donatedOn: Date | null;
donatedOnOverride: Date | null;
amount: number;
amountOverride: number | null;
amountUnassigned: number;
createdAt: Date;
updatedAt: Date;
notes?: string | null;
solicitationCodeOverride?: SolicitationCodeNode | null;
batchType: BatchType;
batchTypeOverride: BatchType | null;
}) {
return FundraisingEntryNode.createInstance().withValues(init);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/common/lib/api/standardResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type CrudResolver<
// Delete Multiple
[K in `delete${Capitalize<PluralNodeName>}`]?: CrudOperations<NodeType>["deleteMultiple"];
} & {
// Update Multiple
// Set Multiple
[K in `set${Capitalize<PluralNodeName>}`]?: CrudOperations<NodeType>["updateMultiple"];
} & {
// Banned operation names
Expand Down Expand Up @@ -125,7 +125,7 @@ export function getCrudOperationNames<
getMultiple: `getMultiple${Capitalize<PluralNodeName>}`;
createMultiple: `create${Capitalize<PluralNodeName>}`;
deleteMultiple: `delete${Capitalize<PluralNodeName>}`;
updateMultiple: `set${Capitalize<PluralNodeName>}`;
setMultiple: `set${Capitalize<PluralNodeName>}`;
} {
const capitalizedNodeName = capitalize(nodeName);
const capitalizedPluralNodeName = capitalize(pluralNodeName);
Expand All @@ -140,6 +140,6 @@ export function getCrudOperationNames<
getMultiple: `getMultiple${capitalizedPluralNodeName}` as const,
createMultiple: `create${capitalizedPluralNodeName}` as const,
deleteMultiple: `delete${capitalizedPluralNodeName}` as const,
updateMultiple: `set${capitalizedPluralNodeName}` as const,
setMultiple: `set${capitalizedPluralNodeName}` as const,
};
}
183 changes: 26 additions & 157 deletions packages/portal/src/config/refine/data.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */
import type { DataProvider } from "@refinedev/core";
import type {
CrudFilter,
CrudOperators,
LogicalFilter,
Pagination,
} from "@refinedev/core";
import type {
AbstractFilteredListQueryArgs,
ListQueryType,
} from "@ukdanceblue/common";
import type { Pagination } from "@refinedev/core";
import type { AbstractFilteredListQueryArgs } from "@ukdanceblue/common";
import { getCrudOperationNames } from "@ukdanceblue/common";
import { gql } from "@urql/core";
import camelcase from "camelcase";
Expand All @@ -21,7 +13,6 @@ import {
type SelectionSetNode,
visit,
} from "graphql";
import set from "lodash/set";
import pluralize, { singular } from "pluralize";

import { API_BASE_URL, urqlClient } from "../api";
Expand Down Expand Up @@ -74,8 +65,8 @@ export const dataProvider: Required<DataProvider> = {

const query = isMutation(gqlOperation)
? gqlButDifferentName`
query Get${camelcase(singular(resource), { pascalCase: true })}($id: ID!) {
${camelcase(singular(resource))}(id: $id) {
query Get${camelcase(singular(resource), { pascalCase: true })}($id: GlobalID!) {
${getOperationName(resource, "getOne")}(id: $id) {
${getOperationFields(gqlOperation)}
}
}
Expand All @@ -102,12 +93,18 @@ export const dataProvider: Required<DataProvider> = {
throw new Error("Operation is required.");
}

if (filters && filters.length > 0) {
throw new Error("Filters are not supported yet.");
}

const response = await urqlClient
.query(meta.gqlQuery, {
sortBy: sorters?.map((sorter) => sorter.field) ?? null,
sortDirection: sorters?.map((sorter) => sorter.order) ?? null,
page: pagination?.current ?? null,
pageSize: pagination?.pageSize ?? null,
...meta?.variables,
...meta?.gqlVariables,
} satisfies Partial<
AbstractFilteredListQueryArgs<
string,
Expand All @@ -121,10 +118,12 @@ export const dataProvider: Required<DataProvider> = {
.toPromise();

console.log(response);
const data = response.data?.[resource].nodes;
const total = response.data?.[resource].totalCount;

return { data, total };
const val = response.data[getOperationName(resource, "getList")];
if (Array.isArray(val)) {
return { data: val, total: val.length };
} else {
return { data: val.data, total: val.total };
}
},

getMany: async (params) => {
Expand All @@ -145,17 +144,22 @@ export const dataProvider: Required<DataProvider> = {

const response = await urqlClient
.mutation(gqlOperation, {
id,
input: {
id,
update: variables,
...meta?.gqlVariables,
...variables,
...meta?.gqlVariables?.input,
},
...meta?.gqlVariables,
})
.toPromise();

const key = `updateOne${camelcase(singular(resource), { pascalCase: true })}`;
const key = getOperationName(resource, "setOne");
const data = response.data?.[key];

if (response.error) {
throw new Error(response.error.message);
}

return { data };
},

Expand All @@ -180,7 +184,7 @@ export const dataProvider: Required<DataProvider> = {
})
.toPromise();

const key = `deleteOne${camelcase(singular(resource), { pascalCase: true })}`;
const key = getOperationName(resource, "deleteOne");
const data = response.data?.[key];

return { data };
Expand Down Expand Up @@ -234,141 +238,6 @@ export const buildPagination = (pagination: Pagination = {}) => {
};
};

const operatorMap: Record<string, string> = {
eq: "eq",
ne: "neq",
lt: "lt",
gt: "gt",
lte: "lte",
gte: "gte",
in: "in",
nin: "notIn",
};

const operatorMapper = (
operator: CrudOperators,
value: any
): Record<string, any> => {
if (operator === "contains") {
return { iLike: `%${value}%` };
}

if (operator === "ncontains") {
return { notILike: `%${value}%` };
}

if (operator === "containss") {
return { like: `%${value}%` };
}

if (operator === "ncontainss") {
return { notLike: `%${value}%` };
}

if (operator === "startswith") {
return { iLike: `${value}%` };
}

if (operator === "nstartswith") {
return { notILike: `${value}%` };
}

if (operator === "startswiths") {
return { like: `${value}%` };
}

if (operator === "nstartswiths") {
return { notLike: `${value}%` };
}

if (operator === "endswith") {
return { iLike: `%${value}` };
}

if (operator === "nendswith") {
return { notILike: `%${value}` };
}

if (operator === "endswiths") {
return { like: `%${value}` };
}

if (operator === "nendswiths") {
return { notLike: `%${value}` };
}

if (operator === "null") {
return { is: null };
}

if (operator === "nnull") {
return { isNot: null };
}

if (operator === "between") {
if (!Array.isArray(value)) {
throw new TypeError("Between operator requires an array");
}

if (value.length !== 2) {
return {};
}

return { between: { lower: value[0], upper: value[1] } };
}

if (operator === "nbetween") {
if (!Array.isArray(value)) {
throw new TypeError("NBetween operator requires an array");
}

if (value.length !== 2) {
return {};
}

return { notBetween: { lower: value[0], upper: value[1] } };
}

return { [operatorMap[operator]!]: value };
};

export const buildFilters = (
filters: LogicalFilter[] | CrudFilter[] = []
): ListQueryType<Record<string, unknown>>["filter"] => {
const result: Record<string, Record<string, string | number>> = {};

filters
.filter((f) => {
if (Array.isArray(f.value) && f.value.length === 0) {
return false;
}
if (typeof f.value === "number") {
return Number.isFinite(f.value);
}

// If the value is null or undefined, it returns false.
return !(f.value == null);
})
.map((filter: LogicalFilter | CrudFilter) => {
if (filter.operator === "and" || filter.operator === "or") {
return set(result, filter.operator, [
buildFilters(filter.value as LogicalFilter[]),
]);
}
if ("field" in filter) {
return set(
result,
filter.field,
operatorMapper(filter.operator, filter.value)
);
}

return {};
});

return result;
};

export const getOperationFields = (documentNode: DocumentNode) => {
const fieldLines: string[] = [];
let isInitialEnter = true;
Expand Down
Loading

0 comments on commit aa8fa35

Please sign in to comment.