mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
fix: preserve datetime timezone on row updates
Datetime inputs are displayed as timezone-less local ISO strings. Without converting back to UTC on submit, unchanged datetime fields drift by the browser offset on every update (console#2870). Mirror the filters path and canonicalize datetime scalars/arrays in buildPayload. Co-authored-by: chiragaggarwal5k <chiragaggarwal5k@gmail.com>
This commit is contained in:
1 parent
f8b969cbb3
commit
0542f386ed
2 files changed
+76
-3
No files matched your search
+49
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { toLocalDateTimeISO } from '$lib/helpers/date';
|
||||
import { buildPayload } from './store';
|
||||
import type { Field } from '$database/(entity)';
|
||||
|
||||
describe('buildPayload', () => {
|
||||
it('converts local datetime-local strings to UTC ISO for datetime fields', () => {
|
||||
const utc = '2026-01-01T00:00:00.000Z';
|
||||
const local = toLocalDateTimeISO(utc);
|
||||
const fields = [{ key: 'startAt', type: 'datetime' }] as Field[];
|
||||
|
||||
const payload = buildPayload(fields, { startAt: local, name: 'unchanged' });
|
||||
|
||||
expect(payload.startAt).toBe(new Date(local).toISOString());
|
||||
expect(payload.name).toBe('unchanged');
|
||||
});
|
||||
|
||||
it('round-trips an unchanged datetime through local display encoding', () => {
|
||||
const utc = '2026-01-01T00:00:00.000Z';
|
||||
const fields = [{ key: 'startAt', type: 'datetime' }] as Field[];
|
||||
|
||||
const payload = buildPayload(fields, { startAt: toLocalDateTimeISO(utc) });
|
||||
|
||||
expect(payload.startAt).toBe(new Date(utc).toISOString());
|
||||
});
|
||||
|
||||
it('converts datetime arrays element-wise', () => {
|
||||
const utc = '2026-01-01T00:00:00.000Z';
|
||||
const local = toLocalDateTimeISO(utc);
|
||||
const fields = [{ key: 'times', type: 'datetime', array: true }] as Field[];
|
||||
|
||||
const payload = buildPayload(fields, { times: [local, null] });
|
||||
|
||||
expect(payload.times).toEqual([new Date(local).toISOString(), null]);
|
||||
});
|
||||
|
||||
it('leaves null and empty datetime values alone', () => {
|
||||
const fields = [{ key: 'startAt', type: 'datetime' }] as Field[];
|
||||
|
||||
expect(buildPayload(fields, { startAt: null }).startAt).toBeNull();
|
||||
expect(buildPayload(fields, { startAt: '' }).startAt).toBe('');
|
||||
});
|
||||
|
||||
it('still stringifies bigint fields', () => {
|
||||
const fields = [{ key: 'count', type: 'bigint' }] as Field[];
|
||||
|
||||
expect(buildPayload(fields, { count: 42n }).count).toBe('42');
|
||||
});
|
||||
});
|
||||
+27
-3
@@ -65,6 +65,29 @@ function castBigIntValue(value: RowValue): RowValue {
|
||||
return String(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Datetime inputs are shown as timezone-less local ISO strings (see
|
||||
* `toLocalDateTimeISO`). Convert them back to UTC before the API call so
|
||||
* unchanged fields do not drift by the browser's UTC offset on every update.
|
||||
*/
|
||||
function castDatetimeValue(value: RowValue): RowValue {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
return value;
|
||||
}
|
||||
|
||||
const date = new Date(value);
|
||||
|
||||
if (isNaN(date.getTime())) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return date.toISOString();
|
||||
}
|
||||
|
||||
export function buildPayload<T extends Record<string, RowValue>>(
|
||||
fields: Field[] | undefined,
|
||||
row: T
|
||||
@@ -72,18 +95,19 @@ export function buildPayload<T extends Record<string, RowValue>>(
|
||||
const payload = structuredClone(row) as Record<string, RowValue>;
|
||||
|
||||
for (const field of fields ?? []) {
|
||||
if (field.type !== 'bigint') {
|
||||
if (field.type !== 'bigint' && field.type !== 'datetime') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const value = payload[field.key];
|
||||
const cast = field.type === 'bigint' ? castBigIntValue : castDatetimeValue;
|
||||
|
||||
if (field.array && Array.isArray(value)) {
|
||||
payload[field.key] = value.map((item) => castBigIntValue(item));
|
||||
payload[field.key] = value.map((item) => cast(item));
|
||||
continue;
|
||||
}
|
||||
|
||||
payload[field.key] = castBigIntValue(value);
|
||||
payload[field.key] = cast(value);
|
||||
}
|
||||
|
||||
return payload as T;
|
||||
|
||||
Reference in new issue
Block a user