Skip to content

Commit

Permalink
toutouyoutou
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Besson committed Apr 11, 2023
1 parent 7ffd2c4 commit 93aabdd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/repository/activity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Except, SetOptional, SetRequired } from 'type-fest';
import isISO8601 from 'validator/lib/isISO8601';
import validator from 'validator';
import { z } from 'zod';

import { LineString } from './geojson.js';
Expand All @@ -12,7 +12,7 @@ export const Activity = z.object({
userId: z.number().int().positive(),
vendor: Vendor,
vendorId: z.string().min(1),
date: z.string().refine(isISO8601, {
date: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
name: z.string().min(1).optional(),
Expand Down
4 changes: 2 additions & 2 deletions src/server/decathlon/decathlon.api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import isISO8601 from 'validator/lib/isISO8601';
import validator from 'validator';
import { z } from 'zod';

import config from '../../config.js';
Expand All @@ -20,7 +20,7 @@ export const Activity = z.object({
id: z.string().min(5).max(100),
name: z.string().max(100).optional(),
sport: z.string().regex(/^\/v2\/sports\/\d+$/),
startdate: z.string().refine(isISO8601, {
startdate: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
duration: z.number().nonnegative().optional(),
Expand Down
16 changes: 7 additions & 9 deletions src/server/polar/polar.api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import axios from 'axios';
import isISO8601 from 'validator/lib/isISO8601';
import isURL from 'validator/lib/isURL';
import isUUID from 'validator/lib/isUUID';
import validator from 'validator';
import { z } from 'zod';

import config from '../../config.js';
Expand Down Expand Up @@ -34,10 +32,10 @@ const CreatedWebhookInfo = z.object({
data: z.object({
id: z.string().min(1).max(50),
events: z.array(WebhookType),
url: z.string().refine(isURL, {
url: z.string().refine(validator.isURL, {
message: 'String must be an URL',
}),
signature_secret_key: z.string().refine(isUUID, {
signature_secret_key: z.string().refine(validator.isUUID, {
message: 'String must be an UUID',
}),
}),
Expand All @@ -50,7 +48,7 @@ const WebhookInfo = z.object({
z.object({
id: z.string(),
events: z.array(WebhookType),
url: z.string().refine(isURL, {
url: z.string().refine(validator.isURL, {
message: 'String must be an URL',
}),
}),
Expand All @@ -61,7 +59,7 @@ export type WebhookInfo = z.infer<typeof WebhookInfo>;

const WebhookPingEvent = z.object({
event: z.literal('PING'),
timestamp: z.string().refine(isISO8601, {
timestamp: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
});
Expand All @@ -82,7 +80,7 @@ const WebhookExerciseEvent = z.object({
})
.pipe(z.bigint()),
entity_id: z.string().min(1).max(50),
timestamp: z.string().refine(isISO8601, {
timestamp: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
});
Expand Down Expand Up @@ -257,7 +255,7 @@ export type SportType = z.infer<typeof SportType>;

const Exercise = z.object({
id: z.string().min(1).max(50),
start_time: z.string().refine(isISO8601, {
start_time: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
start_time_utc_offset: z.number().int(),
Expand Down
30 changes: 15 additions & 15 deletions src/server/polar/polar.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createHmac } from 'crypto';

import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration.js';
import { parse } from 'iso8601-duration';
import { parse, toSeconds } from 'iso8601-duration';
import invariant from 'tiny-invariant';

import { NotFoundError } from '../../errors.js';
Expand All @@ -19,8 +18,6 @@ import { userService } from '../../user.service.js';

import { WebhookEvent, isWebhookPingEvent, polarApi, type Exercise } from './polar.api.js';

dayjs.extend(duration);

export class PolarService {
public async requestAccessTokenAndSetupUser(c2cId: number, authorizationCode: string): Promise<void> {
const auth = await polarApi.exchangeToken(authorizationCode);
Expand Down Expand Up @@ -171,30 +168,33 @@ export class PolarService {
}

private asRepositoryActivity(exercise: Exercise, geojson: LineString): NewActivityWithGeometry {
const startDate = this.localDate(exercise);
return {
vendor: 'polar' as Vendor,
vendorId: exercise.id,
date: this.localDate(exercise),
date: startDate,
type: exercise.sport,
geojson,
...(exercise.distance && { length: Math.round(exercise.distance) }), // float in Polar API, integer in DB
...(exercise.duration && { duration: this.duration(exercise.duration) }), // ISO8601 duration in Polar API
...(exercise.duration && { duration: this.duration(exercise.duration, dayjs(startDate).toDate()) }), // ISO8601 duration in Polar API
};
}

private localDate(exercise: Exercise): string {
const isNegative = exercise.start_time_utc_offset < 0;
const offset = dayjs
.duration({
hours: Math.floor(Math.abs(exercise.start_time_utc_offset) / 60),
minutes: Math.abs(exercise.start_time_utc_offset) % 60,
})
.format('HH:mm');
return exercise.start_time + (isNegative ? '-' : '+') + offset;
const hours = Math.floor(Math.abs(exercise.start_time_utc_offset) / 60);
const minutes = Math.abs(exercise.start_time_utc_offset) % 60;
return (
exercise.start_time +
(isNegative ? '-' : '+') +
hours.toString().padStart(2, '0') +
':' +
minutes.toString().padStart(2, '0')
);
}

private duration(duration: string): number {
return Math.round(dayjs.duration(parse(duration)).asSeconds());
private duration(duration: string, startDate: Date): number {
return Math.round(toSeconds(parse(duration), startDate));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/server/strava/strava.api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import isISO8601 from 'validator/lib/isISO8601';
import validator from 'validator';
import { z } from 'zod';

import config from '../../config.js';
Expand Down Expand Up @@ -87,10 +87,10 @@ export const Activity = z.object({
id: z.number().int().positive(),
name: z.string(),
sport_type: SportType,
start_date: z.string().refine(isISO8601, {
start_date: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
start_date_local: z.string().refine(isISO8601, {
start_date_local: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
distance: z.number().nonnegative(), // in meters
Expand Down Expand Up @@ -138,10 +138,10 @@ export const Subscription = z.object({
id: z.number().int().positive(),
application_id: z.number().int().positive(),
callback_url: z.string().url(),
created_at: z.string().refine(isISO8601, {
created_at: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
updated_at: z.string().refine(isISO8601, {
updated_at: z.string().refine(validator.isISO8601, {
message: 'String must be an ISO-8601 date',
}),
});
Expand Down

0 comments on commit 93aabdd

Please sign in to comment.