From acbe4d122ef7e843d31d1dc12d337204e5f13154 Mon Sep 17 00:00:00 2001 From: James Chang Date: Wed, 24 Mar 2021 18:33:19 -0400 Subject: [PATCH] Add cubing-icon field to event --- .../20210224123833_initial_migration.ts | 1 - .../20210324175644_add_event_cubing_icon.ts | 13 ++++ backend/functions/migration.ts | 2 +- backend/functions/schema.ts | 5 +- .../src/schema/core/services/normal.ts | 2 +- backend/functions/src/schema/helpers/sql.ts | 28 ++++++--- .../links/userUserFollowLink/service.ts | 4 +- .../src/schema/models/event/typeDef.ts | 6 ++ .../src/schema/models/personalBest/service.ts | 10 +-- .../src/schema/models/personalBest/typeDef.ts | 17 +---- .../src/schema/models/user/rootResolver.ts | 63 ------------------- .../special/editPersonalBestInterface.vue | 28 ++++----- .../components/table/common/eventColumn.vue | 2 +- frontend/models/event.ts | 14 ++++- frontend/types/schema.ts | 5 +- 15 files changed, 80 insertions(+), 120 deletions(-) create mode 100644 backend/functions/db/migrations/20210324175644_add_event_cubing_icon.ts diff --git a/backend/functions/db/migrations/20210224123833_initial_migration.ts b/backend/functions/db/migrations/20210224123833_initial_migration.ts index bfe6816..91c2022 100644 --- a/backend/functions/db/migrations/20210224123833_initial_migration.ts +++ b/backend/functions/db/migrations/20210224123833_initial_migration.ts @@ -60,7 +60,6 @@ export async function up(knex: Knex): Promise { table.dateTime("created_at").notNullable().defaultTo(knex.fn.now()); table.dateTime("updated_at").nullable(); table.integer("created_by").notNullable(); - // table.unique(["pb_class", "event", "set_size", "created_by"]); }), ]); } diff --git a/backend/functions/db/migrations/20210324175644_add_event_cubing_icon.ts b/backend/functions/db/migrations/20210324175644_add_event_cubing_icon.ts new file mode 100644 index 0000000..a20c654 --- /dev/null +++ b/backend/functions/db/migrations/20210324175644_add_event_cubing_icon.ts @@ -0,0 +1,13 @@ +import * as Knex from "knex"; + +export async function up(knex: Knex): Promise { + return knex.schema.alterTable("event", function (t) { + t.string("cubing_icon").nullable(); + }); +} + +export async function down(knex: Knex): Promise { + return knex.schema.alterTable("event", function (t) { + t.dropColumn("cubing_icon"); + }); +} diff --git a/backend/functions/migration.ts b/backend/functions/migration.ts index 402b7a7..2274576 100644 --- a/backend/functions/migration.ts +++ b/backend/functions/migration.ts @@ -32,6 +32,7 @@ export async function up(knex: Knex): Promise { table.increments(); table.string("name").notNullable(); table.string("code").notNullable().unique(); + table.string("cubing_icon").nullable(); table.string("score_method").notNullable(); table.dateTime("created_at").notNullable().defaultTo(knex.fn.now()); table.dateTime("updated_at").nullable(); @@ -69,7 +70,6 @@ export async function up(knex: Knex): Promise { table.dateTime("created_at").notNullable().defaultTo(knex.fn.now()); table.dateTime("updated_at").nullable(); table.integer("created_by").notNullable(); - table.unique(["pb_class", "event", "set_size", "created_by"]); }), ]); } diff --git a/backend/functions/schema.ts b/backend/functions/schema.ts index ca9b7d8..29b114d 100644 --- a/backend/functions/schema.ts +++ b/backend/functions/schema.ts @@ -176,7 +176,7 @@ export type FilterByField = { socialLogin: { provider: Scalars["string"]; code: Scalars["string"]; - redirect_uri: Scalars["string"]; + redirectUri: Scalars["string"]; }; event: { id?: Scalars["id"]; code?: Scalars["string"] }; "eventFilterByField/id": FilterByField; @@ -201,11 +201,13 @@ export type FilterByField = { createEvent: { name: Scalars["string"]; code: Scalars["string"]; + cubingIcon?: Scalars["string"] | null; scoreMethod: Scalars["scoreMethod"]; }; updateEventFields: { name?: Scalars["string"]; code?: Scalars["string"]; + cubingIcon?: Scalars["string"] | null; scoreMethod?: Scalars["scoreMethod"]; }; updateEvent: { @@ -539,6 +541,7 @@ export type UserUserFollowLinkEdge = Edge; }; name: { Type: Scalars["string"]; Args: undefined }; code: { Type: Scalars["string"]; Args: undefined }; + cubingIcon: { Type: Scalars["string"] | null; Args: undefined }; scoreMethod: { Type: Scalars["scoreMethod"]; Args: undefined }; /**When the record was created*/ createdAt: { Type: Scalars["unixTimestamp"]; diff --git a/backend/functions/src/schema/core/services/normal.ts b/backend/functions/src/schema/core/services/normal.ts index a0a6902..08f842a 100644 --- a/backend/functions/src/schema/core/services/normal.ts +++ b/backend/functions/src/schema/core/services/normal.ts @@ -751,7 +751,7 @@ export class NormalService extends BaseService { id: item.id, updateFields: { ...validatedArgs.fields, - updated_at: 1, + updatedAt: 1, }, req, fieldPath, diff --git a/backend/functions/src/schema/helpers/sql.ts b/backend/functions/src/schema/helpers/sql.ts index 82de060..f39a865 100644 --- a/backend/functions/src/schema/helpers/sql.ts +++ b/backend/functions/src/schema/helpers/sql.ts @@ -11,14 +11,6 @@ import Knex = require("knex"); import { isDev } from "../../config"; import { executeDBQuery, knex } from "../../utils/knex"; -// options for knex to handle conflicts -/* export type InsertTableRowOptions = { - onConflict: { - columns: string[]; - action: "ignore" | "merge"; - }; -}; */ - type FieldInfo = { alias: string; tableAlias: string; @@ -61,7 +53,9 @@ export type SqlWhereFieldOperator = | "regex" | "like" | "gt" - | "lt"; + | "gte" + | "lt" + | "lte"; export type SqlSelectQuery = { select: SqlSelectQueryObject[]; @@ -283,6 +277,14 @@ function applyWhere( bindings.push(whereSubObject.value); } break; + case "gte": + if (whereSubObject.value === null) { + throw new Error("Can't use this operator with null"); + } else { + whereSubstatement += " >= ?"; + bindings.push(whereSubObject.value); + } + break; case "lt": if (whereSubObject.value === null) { throw new Error("Can't use this operator with null"); @@ -291,6 +293,14 @@ function applyWhere( bindings.push(whereSubObject.value); } break; + case "lte": + if (whereSubObject.value === null) { + throw new Error("Can't use this operator with null"); + } else { + whereSubstatement += " <= ?"; + bindings.push(whereSubObject.value); + } + break; case "in": if (Array.isArray(whereSubObject.value)) { whereSubstatement += ` IN (${whereSubObject.value.map(() => "?")})`; diff --git a/backend/functions/src/schema/links/userUserFollowLink/service.ts b/backend/functions/src/schema/links/userUserFollowLink/service.ts index 4fac197..ab8d137 100644 --- a/backend/functions/src/schema/links/userUserFollowLink/service.ts +++ b/backend/functions/src/schema/links/userUserFollowLink/service.ts @@ -43,13 +43,13 @@ export class UserUserFollowLinkService extends LinkService { }: ServiceFunctionInputs) { // args should be validated already const validatedArgs = args; - await this.handleLookupArgs(args, fieldPath); + await this.handleLookupArgs(validatedArgs, fieldPath); const addResults = await Resolver.createObjectType({ typename: this.typename, addFields: { ...validatedArgs, - created_by: req.user!.id, + createdBy: req.user!.id, }, req, fieldPath, diff --git a/backend/functions/src/schema/models/event/typeDef.ts b/backend/functions/src/schema/models/event/typeDef.ts index 8037429..3f408bc 100644 --- a/backend/functions/src/schema/models/event/typeDef.ts +++ b/backend/functions/src/schema/models/event/typeDef.ts @@ -24,6 +24,12 @@ export default new JomqlObjectType({ unique: true, }, }), + cubingIcon: generateStringField({ + allowNull: true, + sqlOptions: { + field: "cubing_icon", + }, + }), scoreMethod: generateEnumField({ scalarDefinition: Scalars.scoreMethod, allowNull: false, diff --git a/backend/functions/src/schema/models/personalBest/service.ts b/backend/functions/src/schema/models/personalBest/service.ts index c90b40a..b80d537 100644 --- a/backend/functions/src/schema/models/personalBest/service.ts +++ b/backend/functions/src/schema/models/personalBest/service.ts @@ -267,7 +267,7 @@ export class PersonalBestService extends PaginatedService { fields: [ { field: "happenedOn", - operator: "lt", + operator: "lte", value: validatedArgs.happenedOn, }, { @@ -328,7 +328,7 @@ export class PersonalBestService extends PaginatedService { fields: [ { field: "happenedOn", - operator: "gt", + operator: "gte", value: validatedArgs.happenedOn, }, { @@ -421,12 +421,6 @@ export class PersonalBestService extends PaginatedService { }, req, fieldPath, - /* options: { - onConflict: { - columns: ["pbClass", "event", "setSize", "createdBy"], - action: "merge", - }, - }, */ }); return this.getRecord({ diff --git a/backend/functions/src/schema/models/personalBest/typeDef.ts b/backend/functions/src/schema/models/personalBest/typeDef.ts index 81c1047..1431b03 100644 --- a/backend/functions/src/schema/models/personalBest/typeDef.ts +++ b/backend/functions/src/schema/models/personalBest/typeDef.ts @@ -15,6 +15,7 @@ import { generateIntegerField, generateUnixTimestampField, generateBooleanField, + generateCreatedByField, } from "../../helpers/typeDef"; export default new JomqlObjectType({ @@ -27,21 +28,16 @@ export default new JomqlObjectType({ service: PersonalBestClass, allowNull: false, sqlOptions: { - unique: "compositeIndex", field: "pb_class", }, }), event: generateJoinableField({ service: Event, allowNull: false, - sqlOptions: { - unique: "compositeIndex", - }, }), setSize: generateIntegerField({ allowNull: false, sqlOptions: { - unique: "compositeIndex", field: "set_size", }, }), @@ -84,15 +80,6 @@ export default new JomqlObjectType({ }), ...generateCreatedAtField(), ...generateUpdatedAtField(), - // ...generateCreatedByField(User), - createdBy: generateJoinableField({ - service: User, - allowNull: false, - typeDefOptions: { addable: false, updateable: false }, - sqlOptions: { - unique: "compositeIndex", - field: "created_by", - }, - }), + ...generateCreatedByField(User), }, }); diff --git a/backend/functions/src/schema/models/user/rootResolver.ts b/backend/functions/src/schema/models/user/rootResolver.ts index ff82d2c..e8df77e 100644 --- a/backend/functions/src/schema/models/user/rootResolver.ts +++ b/backend/functions/src/schema/models/user/rootResolver.ts @@ -1,8 +1,6 @@ import { User } from "../../services"; import { generateBaseRootResolvers } from "../../helpers/rootResolver"; import { JomqlRootResolverType } from "jomql"; -import { Scalars } from "../.."; -import * as sqlHelper from "../../helpers/sql"; export default { getCurrentUser: new JomqlRootResolverType({ @@ -26,67 +24,6 @@ export default { // always allow user to get own user }, }), - test: new JomqlRootResolverType({ - name: "test", - allowNull: true, - type: Scalars.unknown, - resolver: async ({ req, fieldPath, args, query }) => { - const results = await sqlHelper.fetchTableRows( - { - select: [ - { field: "id" }, - { field: "name" }, - { field: "bar.id" }, - { field: "bar.bar.id" }, - { field: "created_by.id" }, - { field: "created_at" }, - { field: "current_user_following" }, - ], - from: "user", - where: { - fields: [ - { - field: "id", - operator: "in", - value: [8], - }, - ], - }, - orderBy: [ - { - field: "created_at", - desc: true, - }, - ], - specialParams: { - currentUserId: 7, - }, - distinct: true, - }, - [] - ); - /* const userResults = await sqlHelper.fetchTableRows({ - select: [{ field: "role" }, { field: "permissions" }], - from: User.typename, - where: { - fields: [{ field: "id", value: contextUser.id }], - }, - }); */ - - /* const results = await knex("user").countDistinct("id").where({ - id: 7, - }); */ - - /* const results = await knex - .select({ - created_at: knex.raw(`extract(epoch from user0.created_at)`), - }) - .from({ user0: "user" }) - .leftJoin({ user1: "user" }, "user0.created_by", "user1.id"); */ - - return results; - }, - }), ...generateBaseRootResolvers(User, [ "get", "getMultiple", diff --git a/frontend/components/interface/crud/special/editPersonalBestInterface.vue b/frontend/components/interface/crud/special/editPersonalBestInterface.vue index 59f26ee..bbe32a4 100644 --- a/frontend/components/interface/crud/special/editPersonalBestInterface.vue +++ b/frontend/components/interface/crud/special/editPersonalBestInterface.vue @@ -45,9 +45,9 @@ import editRecordInterfaceMixin from '~/mixins/editRecordInterface' import { isObject } from '~/services/base' const scoreMethodHiddenFieldsMap = { - STANDARD: ['moves_count', 'attempts_succeeded', 'attempts_total'], - FMC: ['time_elapsed', 'attempts_succeeded', 'attempts_total'], - MBLD: ['moves_count'], + STANDARD: ['movesCount', 'attemptsSucceeded', 'attemptsTotal'], + FMC: ['timeElapsed', 'attemptsSucceeded', 'attemptsTotal'], + MBLD: ['movesCount'], } export default { @@ -64,7 +64,7 @@ export default { pbClass() { try { - return this.getInputValue('pb_class.id') + return this.getInputValue('pbClass.id') } catch { return null } @@ -78,14 +78,14 @@ export default { if (this.event) { let scoreMethod if (isObject(this.event)) { - scoreMethod = this.event.score_method + scoreMethod = this.event.scoreMethod } else { const foundEventObject = this.getInputObject( 'event.id' ).options.find((ele) => ele.id === this.event) if (foundEventObject) { - scoreMethod = foundEventObject.score_method + scoreMethod = foundEventObject.scoreMethod } } @@ -93,17 +93,17 @@ export default { } if (this.pbClass) { - // if pbClass.set_size, also hide that + // if pbClass.setSize, also hide that let setSize if (isObject(this.pbClass)) { - setSize = this.pbClass.set_size + setSize = this.pbClass.setSize } else { - setSize = this.getInputObject('pb_class.id').options.find( + setSize = this.getInputObject('pbClass.id').options.find( (ele) => ele.id === this.pbClass // eslint-disable-next-line camelcase - )?.set_size + )?.setSize } - if (setSize) hiddenFields.push('set_size') + if (setSize) hiddenFields.push('setSize') } return !hiddenFields.includes(ele.field) @@ -115,10 +115,10 @@ export default { pbClass(val) { if (!isObject(val)) return // eslint-disable-next-line camelcase - if (!val?.set_size) { - this.setInputValue('set_size', null) + if (!val?.setSize) { + this.setInputValue('setSize', null) } else { - this.setInputValue('set_size', val.set_size) + this.setInputValue('setSize', val.setSize) } }, }, diff --git a/frontend/components/table/common/eventColumn.vue b/frontend/components/table/common/eventColumn.vue index 8bc840d..fc96416 100644 --- a/frontend/components/table/common/eventColumn.vue +++ b/frontend/components/table/common/eventColumn.vue @@ -1,7 +1,7 @@