Skip to content

Commit

Permalink
Make happenedOn an optional field -- automatically set to current time
Browse files Browse the repository at this point in the history
  • Loading branch information
big213 committed Apr 14, 2021
1 parent 1e3b6f0 commit 40af8c2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backend/functions/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function up(knex: Knex): Promise<void[]> {
table.integer("attempts_succeeded").nullable();
table.integer("attempts_total").nullable();
table.integer("product").nullable();
table.dateTime("happened_on").notNullable();
table.dateTime("happened_on").nullable();
table.integer("time_elapsed").nullable();
table.decimal("moves_count").nullable();
table.boolean("is_current").notNullable();
Expand Down
12 changes: 8 additions & 4 deletions backend/functions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export type FilterByField<T> = {
attemptsSucceeded?: Scalars["number"] | null;
attemptsTotal?: Scalars["number"] | null;
product?: InputTypes["product"] | null;
happenedOn: Scalars["unixTimestamp"];
happenedOn?: Scalars["unixTimestamp"] | null;
timeElapsed?: Scalars["number"] | null;
movesCount?: Scalars["number"] | null;
publicComments?: Scalars["string"] | null;
Expand All @@ -342,7 +342,7 @@ export type FilterByField<T> = {
attemptsSucceeded?: Scalars["number"] | null;
attemptsTotal?: Scalars["number"] | null;
product?: InputTypes["product"] | null;
happenedOn?: Scalars["unixTimestamp"];
happenedOn?: Scalars["unixTimestamp"] | null;
timeElapsed?: Scalars["number"] | null;
movesCount?: Scalars["number"] | null;
publicComments?: Scalars["string"] | null;
Expand All @@ -353,7 +353,11 @@ export type FilterByField<T> = {
};
apiKey: { id?: Scalars["id"] };
"apiKeyFilterByField/id": FilterByField<Scalars["id"]>;
apiKeyFilterByObject: { id?: InputTypes["apiKeyFilterByField/id"] };
"apiKeyFilterByField/user.id": FilterByField<Scalars["id"]>;
apiKeyFilterByObject: {
id?: InputTypes["apiKeyFilterByField/id"];
"user.id"?: InputTypes["apiKeyFilterByField/user.id"];
};
apiKeyPaginator: {
first?: Scalars["number"];
last?: Scalars["number"];
Expand Down Expand Up @@ -686,7 +690,7 @@ export type UserUserFollowLinkEdge = Edge<UserUserFollowLink>;
Args: undefined;
};
product: { Type: Product | null; Args: undefined };
happenedOn: { Type: Scalars["unixTimestamp"]; Args: undefined };
happenedOn: { Type: Scalars["unixTimestamp"] | null; Args: undefined };
/**The amount of ms time elapsed for the pb attempt*/ timeElapsed: {
Type: Scalars["number"] | null;
Args: undefined;
Expand Down
1 change: 0 additions & 1 deletion backend/functions/src/helpers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export async function validateApiKey(auth: string): Promise<ContextUser> {
permissions: finalPermissions,
};
} catch (err) {
console.log(err);
const message = "Token error: " + (err.message || err.name);
throw new Error(message);
}
Expand Down
5 changes: 5 additions & 0 deletions backend/functions/src/schema/models/personalBest/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ export class PersonalBestService extends PaginatedService {
// args should be validated already
const validatedArgs = <any>args;

// if happenedOn is null, set it to current unix timestamp
if (!validatedArgs.happenedOn) {
validatedArgs.happenedOn = new Date().getTime() / 1000;
}

// get event.scoreMethod
const eventRecords = await sqlHelper.fetchTableRows({
select: [
Expand Down
2 changes: 2 additions & 0 deletions backend/functions/src/schema/models/personalBest/typeDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "../../core/helpers/typeDef";
import * as Scalars from "../../scalars";
import * as Resolver from "../../core/helpers/resolver";
import { knex } from "../../../utils/knex";

export default new GiraffeqlObjectType(<ObjectTypeDefinition>{
name: PersonalBest.typename,
Expand Down Expand Up @@ -65,6 +66,7 @@ export default new GiraffeqlObjectType(<ObjectTypeDefinition>{
}),
happenedOn: generateUnixTimestampField({
allowNull: false,
defaultValue: knex.fn.now(), // not really setting via DB. default is calculated manually in the createRecord function
sqlOptions: { field: "happened_on" },
}),
timeElapsed: generateIntegerField({
Expand Down
9 changes: 5 additions & 4 deletions frontend/models/personalBest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ export const PersonalBest = <RecordInfo<'personalBest'>>{
},
happenedOn: {
text: 'Date Happened',
optional: true,
hint:
'To specify the exact date and time, use format: YYYY-MM-DD 1:23 PM',
'Leave this blank to set to current date. To specify the exact date and time, use format: YYYY-MM-DD 1:23 PM',
inputType: 'datepicker',
// default to today.
default: () => {
/* default: () => {
const date = new Date()
return (
date.getFullYear() +
Expand All @@ -185,14 +186,14 @@ export const PersonalBest = <RecordInfo<'personalBest'>>{
'-' +
String(date.getDate()).padStart(2, '0')
)
},
}, */
// unix timestamp to YYYY-MM-DD
serialize: (val: number) =>
val && new Date(val * 1000).toISOString().substring(0, 10),
// YYYY-MM-DD to unix timestamp
parseValue: (val: string) => {
// null or falsey values not allowed
if (!val) throw new Error(`Invalid value for Date Happened`)
if (!val) return undefined

const msTimestamp = new Date(val).getTime()
// date cannot be to far in the future
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/my-apps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
. The API Key needs to be passed as a header with the key
<span class="preformatted">x-api-key</span>
. The request body must be sent as JSON. Official documentation of
all the API functiaonlity is not currently available, but you can
all the API functionality is not currently available, but you can
see what the JSON body looks like for various kinds of actions by
looking at the Request Payload in the developer console of your
browser while performing those actions on the site. For help
Expand Down

0 comments on commit 40af8c2

Please sign in to comment.