fix: 🐛 Date non settate tramite API
This commit is contained in:
parent
1cf69e1235
commit
9abdf37efa
|
@ -4,6 +4,7 @@ import {
|
||||||
PaginationStrategy,
|
PaginationStrategy,
|
||||||
PluralResponse
|
PluralResponse
|
||||||
} from 'coloquent';
|
} from 'coloquent';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
import type {ValueOf} from 'type-fest';
|
import type {ValueOf} from 'type-fest';
|
||||||
|
|
||||||
export interface ModelAttributes {
|
export interface ModelAttributes {
|
||||||
|
@ -62,16 +63,17 @@ export default abstract class Model<A extends ModelAttributes, R extends ModelRe
|
||||||
return super.getAttribute(attributeName as string) as ValueOf<A, AN>;
|
return super.getAttribute(attributeName as string) as ValueOf<A, AN>;
|
||||||
}
|
}
|
||||||
|
|
||||||
isDateAttribute(attributeName: string) {
|
|
||||||
// @ts-ignore
|
|
||||||
return super.isDateAttribute(attributeName) && this.attributes.get(attributeName);
|
|
||||||
}
|
|
||||||
|
|
||||||
getAttributes() {
|
getAttributes() {
|
||||||
return super.getAttributes() as ModelAttributes;
|
return super.getAttributes() as ModelAttributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
setAttribute<AN extends keyof A = keyof A>(attributeName: AN, value: ValueOf<A, AN>) {
|
setAttribute<AN extends keyof A = keyof A>(attributeName: AN, value: ValueOf<A, AN>) {
|
||||||
|
const date = dayjs(value as string | Date | undefined);
|
||||||
|
// @ts-expect-error
|
||||||
|
if (this.isDateAttribute(attributeName) && date.isValid()) {
|
||||||
|
// @ts-expect-error
|
||||||
|
value = date.format((this as Model<any>).constructor.dates[attributeName]);
|
||||||
|
}
|
||||||
// @ts-expect-error — This is needed to parse the dates correctly.
|
// @ts-expect-error — This is needed to parse the dates correctly.
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
||||||
this.attributes.set(attributeName as string, value);
|
this.attributes.set(attributeName as string, value);
|
||||||
|
|
Loading…
Reference in New Issue