Skip to content

Commit

Permalink
Add cubing-icon field to event
Browse files Browse the repository at this point in the history
  • Loading branch information
big213 committed Mar 24, 2021
1 parent 027c127 commit acbe4d1
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export async function up(knex: Knex): Promise<void[]> {
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"]);
}),
]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Knex from "knex";

export async function up(knex: Knex): Promise<void> {
return knex.schema.alterTable("event", function (t) {
t.string("cubing_icon").nullable();
});
}

export async function down(knex: Knex): Promise<void> {
return knex.schema.alterTable("event", function (t) {
t.dropColumn("cubing_icon");
});
}
2 changes: 1 addition & 1 deletion backend/functions/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function up(knex: Knex): Promise<void[]> {
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();
Expand Down Expand Up @@ -69,7 +70,6 @@ export async function up(knex: Knex): Promise<void[]> {
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"]);
}),
]);
}
Expand Down
5 changes: 4 additions & 1 deletion backend/functions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export type FilterByField<T> = {
socialLogin: {
provider: Scalars["string"];
code: Scalars["string"];
redirect_uri: Scalars["string"];
redirectUri: Scalars["string"];
};
event: { id?: Scalars["id"]; code?: Scalars["string"] };
"eventFilterByField/id": FilterByField<Scalars["id"]>;
Expand All @@ -201,11 +201,13 @@ export type FilterByField<T> = {
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: {
Expand Down Expand Up @@ -539,6 +541,7 @@ export type UserUserFollowLinkEdge = Edge<UserUserFollowLink>;
};
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"];
Expand Down
2 changes: 1 addition & 1 deletion backend/functions/src/schema/core/services/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ export class NormalService extends BaseService {
id: item.id,
updateFields: {
...validatedArgs.fields,
updated_at: 1,
updatedAt: 1,
},
req,
fieldPath,
Expand Down
28 changes: 19 additions & 9 deletions backend/functions/src/schema/helpers/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +53,9 @@ export type SqlWhereFieldOperator =
| "regex"
| "like"
| "gt"
| "lt";
| "gte"
| "lt"
| "lte";

export type SqlSelectQuery = {
select: SqlSelectQueryObject[];
Expand Down Expand Up @@ -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");
Expand All @@ -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(() => "?")})`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export class UserUserFollowLinkService extends LinkService {
}: ServiceFunctionInputs) {
// args should be validated already
const validatedArgs = <any>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,
Expand Down
6 changes: 6 additions & 0 deletions backend/functions/src/schema/models/event/typeDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export default new JomqlObjectType(<ObjectTypeDefinition>{
unique: true,
},
}),
cubingIcon: generateStringField({
allowNull: true,
sqlOptions: {
field: "cubing_icon",
},
}),
scoreMethod: generateEnumField({
scalarDefinition: Scalars.scoreMethod,
allowNull: false,
Expand Down
10 changes: 2 additions & 8 deletions backend/functions/src/schema/models/personalBest/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class PersonalBestService extends PaginatedService {
fields: [
{
field: "happenedOn",
operator: "lt",
operator: "lte",
value: validatedArgs.happenedOn,
},
{
Expand Down Expand Up @@ -328,7 +328,7 @@ export class PersonalBestService extends PaginatedService {
fields: [
{
field: "happenedOn",
operator: "gt",
operator: "gte",
value: validatedArgs.happenedOn,
},
{
Expand Down Expand Up @@ -421,12 +421,6 @@ export class PersonalBestService extends PaginatedService {
},
req,
fieldPath,
/* options: {
onConflict: {
columns: ["pbClass", "event", "setSize", "createdBy"],
action: "merge",
},
}, */
});

return this.getRecord({
Expand Down
17 changes: 2 additions & 15 deletions backend/functions/src/schema/models/personalBest/typeDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
generateIntegerField,
generateUnixTimestampField,
generateBooleanField,
generateCreatedByField,
} from "../../helpers/typeDef";

export default new JomqlObjectType(<ObjectTypeDefinition>{
Expand All @@ -27,21 +28,16 @@ export default new JomqlObjectType(<ObjectTypeDefinition>{
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",
},
}),
Expand Down Expand Up @@ -84,15 +80,6 @@ export default new JomqlObjectType(<ObjectTypeDefinition>{
}),
...generateCreatedAtField(),
...generateUpdatedAtField(),
// ...generateCreatedByField(User),
createdBy: generateJoinableField({
service: User,
allowNull: false,
typeDefOptions: { addable: false, updateable: false },
sqlOptions: {
unique: "compositeIndex",
field: "created_by",
},
}),
...generateCreatedByField(User),
},
});
63 changes: 0 additions & 63 deletions backend/functions/src/schema/models/user/rootResolver.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -64,7 +64,7 @@ export default {
pbClass() {
try {
return this.getInputValue('pb_class.id')
return this.getInputValue('pbClass.id')
} catch {
return null
}
Expand All @@ -78,32 +78,32 @@ 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
}
}
hiddenFields.push(...(scoreMethodHiddenFieldsMap[scoreMethod] ?? []))
}
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)
Expand All @@ -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)
}
},
},
Expand Down
Loading

0 comments on commit acbe4d1

Please sign in to comment.