From 4723d169f90dfa5ecb3b8362cbac83ced6aafb1e Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 7 Jul 2022 16:58:58 +0530 Subject: [PATCH] fix: don't reuse NumberSeries id --- fyo/models/NumberSeries.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/fyo/models/NumberSeries.ts b/fyo/models/NumberSeries.ts index 1f5f926f..b3fa8244 100644 --- a/fyo/models/NumberSeries.ts +++ b/fyo/models/NumberSeries.ts @@ -7,8 +7,16 @@ function getPaddedName(prefix: string, next: number, padZeros: number): string { export default class NumberSeries extends Doc { setCurrent() { let current = this.get('current') as number | null; + + /** + * Increment current if it isn't the first entry. This + * is to prevent reassignment of NumberSeries document ids. + */ + if (!current) { current = this.get('start') as number; + } else { + current = current + 1; } this.current = current; @@ -16,13 +24,12 @@ export default class NumberSeries extends Doc { async next(schemaName: string) { this.setCurrent(); - const exists = await this.checkIfCurrentExists(schemaName); - if (!exists) { - return this.getPaddedName(this.current as number); + + if (exists) { + this.current = (this.current as number) + 1; } - this.current = (this.current as number) + 1; await this.sync(); return this.getPaddedName(this.current as number); }