2
0
mirror of https://github.com/frappe/books.git synced 2025-04-02 16:21:52 +00:00

fix: prevent converter errors on duplication

This commit is contained in:
18alantom 2022-10-13 17:10:28 +05:30
parent 71a3a45b99
commit 819e6821e1
2 changed files with 22 additions and 8 deletions

View File

@ -6,7 +6,7 @@ import Observable from 'fyo/utils/observable';
import { Schema } from 'schemas/types'; import { Schema } from 'schemas/types';
import { getRandomString } from 'utils'; import { getRandomString } from 'utils';
import { Fyo } from '..'; import { Fyo } from '..';
import { DocValueMap } from './types'; import { DocValueMap, RawValueMap } from './types';
export class DocHandler { export class DocHandler {
fyo: Fyo; fyo: Fyo;
@ -79,10 +79,11 @@ export class DocHandler {
getNewDoc( getNewDoc(
schemaName: string, schemaName: string,
data: DocValueMap = {}, data: DocValueMap | RawValueMap = {},
cacheDoc: boolean = true, cacheDoc: boolean = true,
schema?: Schema, schema?: Schema,
Model?: typeof Doc Model?: typeof Doc,
isRawValueMap: boolean = true
): Doc { ): Doc {
if (!this.models[schemaName] && Model) { if (!this.models[schemaName] && Model) {
this.models[schemaName] = Model; this.models[schemaName] = Model;
@ -95,7 +96,7 @@ export class DocHandler {
throw new NotFoundError(`Schema not found for ${schemaName}`); throw new NotFoundError(`Schema not found for ${schemaName}`);
} }
const doc = new Model!(schema, data, this.fyo); const doc = new Model!(schema, data, this.fyo, isRawValueMap);
doc.name ??= getRandomString(); doc.name ??= getRandomString();
if (cacheDoc) { if (cacheDoc) {
this.#addToCache(doc); this.#addToCache(doc);

View File

@ -1,6 +1,6 @@
import { Fyo } from 'fyo'; import { Fyo } from 'fyo';
import { Converter } from 'fyo/core/converter'; import { Converter } from 'fyo/core/converter';
import { DocValue, DocValueMap } from 'fyo/core/types'; import { DocValue, DocValueMap, RawValueMap } from 'fyo/core/types';
import { Verb } from 'fyo/telemetry/types'; import { Verb } from 'fyo/telemetry/types';
import { DEFAULT_USER } from 'fyo/utils/consts'; import { DEFAULT_USER } from 'fyo/utils/consts';
import { ConflictError, MandatoryError, NotFoundError } from 'fyo/utils/errors'; import { ConflictError, MandatoryError, NotFoundError } from 'fyo/utils/errors';
@ -66,7 +66,12 @@ export class Doc extends Observable<DocValue | Doc[]> {
_notInserted: boolean = true; _notInserted: boolean = true;
_syncing = false; _syncing = false;
constructor(schema: Schema, data: DocValueMap, fyo: Fyo) { constructor(
schema: Schema,
data: DocValueMap,
fyo: Fyo,
convertToDocValue: boolean = true
) {
super(); super();
this.fyo = markRaw(fyo); this.fyo = markRaw(fyo);
this.schema = schema; this.schema = schema;
@ -77,7 +82,7 @@ export class Doc extends Observable<DocValue | Doc[]> {
} }
this._setDefaults(); this._setDefaults();
this._setValuesWithoutChecks(data, true); this._setValuesWithoutChecks(data, convertToDocValue);
} }
get schemaName(): string { get schemaName(): string {
@ -364,6 +369,9 @@ export class Doc extends Observable<DocValue | Doc[]> {
const childDoc = this.fyo.doc.getNewDoc( const childDoc = this.fyo.doc.getNewDoc(
childSchemaName, childSchemaName,
docValueMap, docValueMap,
false,
undefined,
undefined,
false false
); );
childDoc.parentdoc = this; childDoc.parentdoc = this;
@ -913,7 +921,12 @@ export class Doc extends Observable<DocValue | Doc[]> {
updateMap.name = updateMap.name + ' CPY'; updateMap.name = updateMap.name + ' CPY';
} }
return this.fyo.doc.getNewDoc(this.schemaName, updateMap); const rawUpdateMap = this.fyo.db.converter.toRawValueMap(
this.schemaName,
updateMap
) as RawValueMap;
return this.fyo.doc.getNewDoc(this.schemaName, rawUpdateMap, true);
} }
/** /**