From e6aa55250aa2b112dd5d9b0eec38aab9147f2982 Mon Sep 17 00:00:00 2001 From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> Date: Mon, 11 Jul 2022 12:35:27 +0600 Subject: [PATCH] Fix schedule timezone (#2234) Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> --- models/hr/src/migration.ts | 118 +++++++++++++++++- .../src/components/CreateRequest.svelte | 24 ++-- .../src/components/ScheduleView.svelte | 2 +- 3 files changed, 133 insertions(+), 11 deletions(-) diff --git a/models/hr/src/migration.ts b/models/hr/src/migration.ts index ca398eda87..a873b1e31f 100644 --- a/models/hr/src/migration.ts +++ b/models/hr/src/migration.ts @@ -13,10 +13,11 @@ // limitations under the License. // -import { TxOperations } from '@anticrm/core' +import { DOMAIN_TX, SortingOrder, TxCreateDoc, TxOperations, TxUpdateDoc } from '@anticrm/core' +import { Request } from '@anticrm/hr' import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@anticrm/model' import core from '@anticrm/model-core' -import hr from './index' +import hr, { DOMAIN_HR } from './index' async function createSpace (tx: TxOperations): Promise { const current = await tx.findOne(core.class.Space, { @@ -39,8 +40,119 @@ async function createSpace (tx: TxOperations): Promise { } } +function toUTC (date: Date | number): number { + const res = new Date(date) + if (res.getUTCFullYear() !== res.getFullYear()) { + res.setUTCFullYear(res.getFullYear()) + } + if (res.getUTCMonth() !== res.getMonth()) { + res.setUTCMonth(res.getMonth()) + } + if (res.getUTCDate() !== res.getDate()) { + res.setUTCDate(res.getDate()) + } + return res.setUTCHours(12, 0, 0, 0) +} + +function isDefault (date: number, due: number): boolean { + const start = new Date(date) + const end = new Date(due) + if (start.getDate() === end.getDate() && end.getHours() - start.getHours() === 12) { + return true + } + if (start.getDate() + 1 === end.getDate() && end.getHours() === start.getHours()) { + return true + } + return false +} + +async function migrateRequestTime (client: MigrationClient, request: Request): Promise { + const date = toUTC(request.date) + const dueDate = isDefault(request.date, request.dueDate) ? date : toUTC(request.dueDate) + await client.update( + DOMAIN_HR, + { _id: request._id }, + { + date, + dueDate + } + ) + + const updateDateTx = ( + await client.find>( + DOMAIN_TX, + { _class: core.class.TxUpdateDoc, objectId: request._id, 'operations.date': { $exists: true } }, + { sort: { modifiedOn: SortingOrder.Descending } } + ) + )[0] + if (updateDateTx !== undefined) { + const operations = updateDateTx.operations + operations.dueDate = date + await client.update( + DOMAIN_TX, + { _id: updateDateTx._id }, + { + operations + } + ) + } + const updateDueTx = ( + await client.find>( + DOMAIN_TX, + { _class: core.class.TxUpdateDoc, objectId: request._id, 'operations.dueDate': { $exists: true } }, + { sort: { modifiedOn: SortingOrder.Descending } } + ) + )[0] + if (updateDueTx !== undefined) { + const operations = updateDueTx.operations + operations.dueDate = dueDate + await client.update( + DOMAIN_TX, + { _id: updateDateTx._id }, + { + operations + } + ) + } + + if (updateDueTx === undefined || updateDateTx === undefined) { + const createTx = ( + await client.find>( + DOMAIN_TX, + { _class: core.class.TxCreateDoc, objectId: request._id }, + { sort: { modifiedOn: SortingOrder.Descending } } + ) + )[0] + if (createTx !== undefined) { + const attributes = createTx.attributes + if (updateDateTx === undefined) { + attributes.date = date + } + if (updateDueTx === undefined) { + attributes.dueDate = dueDate + } + await client.update( + DOMAIN_TX, + { _id: createTx._id }, + { + attributes + } + ) + } + } +} + +async function migrateTime (client: MigrationClient): Promise { + const requests = await client.find(DOMAIN_HR, { _class: hr.class.Request }) + for (const request of requests) { + await migrateRequestTime(client, request) + } +} + export const hrOperation: MigrateOperation = { - async migrate (client: MigrationClient): Promise {}, + async migrate (client: MigrationClient): Promise { + await migrateTime(client) + }, async upgrade (client: MigrationUpgradeClient): Promise { const tx = new TxOperations(client, core.account.System) await createSpace(tx) diff --git a/plugins/hr-resources/src/components/CreateRequest.svelte b/plugins/hr-resources/src/components/CreateRequest.svelte index b6c1931887..23b221fdd3 100644 --- a/plugins/hr-resources/src/components/CreateRequest.svelte +++ b/plugins/hr-resources/src/components/CreateRequest.svelte @@ -47,12 +47,26 @@ }) let value = new Date(date).getTime() - $: dueDate = new Date(value).setDate(new Date(value).getDate() + 1) + $: dueDate = new Date(value).getTime() export function canClose (): boolean { return description.length === 0 } + function toUTC (date: Date | number): number { + const res = new Date(date) + if (res.getUTCFullYear() !== res.getFullYear()) { + res.setUTCFullYear(res.getFullYear()) + } + if (res.getUTCMonth() !== res.getMonth()) { + res.setUTCMonth(res.getMonth()) + } + if (res.getUTCDate() !== res.getDate()) { + res.setUTCDate(res.getDate()) + } + return res.setUTCHours(12, 0, 0, 0) + } + async function saveRequest () { let date: number | undefined if (value != null) date = value @@ -62,8 +76,8 @@ attachedTo: staff._id, attachedToClass: staff._class, type: type._id, - date, - dueDate, + date: toUTC(date), + dueDate: toUTC(dueDate), description, collection: 'requests' }) @@ -72,10 +86,6 @@ function typeSelected (_id: Ref): void { type = types.find((p) => p._id === _id) - dueDate = - Math.abs(type?.value ?? 0 % 1) === 0.5 - ? new Date(value).setHours(12) - : new Date(value).setDate(new Date(value).getDate() + 1) } diff --git a/plugins/hr-resources/src/components/ScheduleView.svelte b/plugins/hr-resources/src/components/ScheduleView.svelte index a0a7fae28e..757bb94511 100644 --- a/plugins/hr-resources/src/components/ScheduleView.svelte +++ b/plugins/hr-resources/src/components/ScheduleView.svelte @@ -115,7 +115,7 @@ const time = date.getTime() const endTime = getEndDate(date) for (const request of requests) { - if (request.date < endTime && request.dueDate > time) { + if (request.date <= endTime && request.dueDate > time) { res.push(request) } }