Fix schedule timezone (#2234)

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov 2022-07-11 12:35:27 +06:00 committed by GitHub
parent 5734d297b5
commit e6aa55250a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 133 additions and 11 deletions

View File

@ -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<void> {
const current = await tx.findOne(core.class.Space, {
@ -39,8 +40,119 @@ async function createSpace (tx: TxOperations): Promise<void> {
}
}
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<void> {
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<TxUpdateDoc<Request>>(
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<TxUpdateDoc<Request>>(
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<TxCreateDoc<Request>>(
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<void> {
const requests = await client.find<Request>(DOMAIN_HR, { _class: hr.class.Request })
for (const request of requests) {
await migrateRequestTime(client, request)
}
}
export const hrOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async migrate (client: MigrationClient): Promise<void> {
await migrateTime(client)
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
const tx = new TxOperations(client, core.account.System)
await createSpace(tx)

View File

@ -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<RequestType>): 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)
}
</script>

View File

@ -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)
}
}