Skip to content

Commit

Permalink
fix: auto number reset unexpectedly (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
tea-artist authored Nov 22, 2024
1 parent d4b0b96 commit d5e0184
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ export class ReferenceService {
} = props;
const dbTableName = fieldId2DbTableName[field.id];

const recordIds = relatedRecordItems.map((item) => item.toId);
const foreignRecordIds = relatedRecordItems
.map((item) => item.fromId)
.filter(Boolean) as string[];
const recordIds = uniq(relatedRecordItems.map((item) => item.toId));
const foreignRecordIds = uniq(
relatedRecordItems.map((item) => item.fromId).filter(Boolean) as string[]
);

// record data source
const recordMapByTableName = await this.getRecordMapBatch({
Expand Down Expand Up @@ -269,7 +269,7 @@ export class ReferenceService {
} = props;

const dbTableName = fieldId2DbTableName[field.id];
const recordIds = relatedRecordItems.map((item) => item.toId);
const recordIds = uniq(relatedRecordItems.map((item) => item.toId));

// record data source
const recordIdsByTableName = await this.getRecordMapBatch({
Expand All @@ -287,9 +287,9 @@ export class ReferenceService {
? await this.getUserMap(recordMap, field.type)
: undefined;

const changes = Object.values(recordMap).reduce<ICellChange[]>((pre, record) => {
const changes = recordIds.reduce<ICellChange[]>((pre, recordId) => {
const record = recordMap[recordId];
const change = this.collectChanges({ record }, tableId, field, fieldMap, userMap);

if (change) {
pre.push(change);
}
Expand Down Expand Up @@ -374,7 +374,7 @@ export class ReferenceService {
});
}

recordIdsMap[fieldId] = Array.from(new Set(relatedRecordItems.map((item) => item.toId)));
recordIdsMap[fieldId] = uniq(relatedRecordItems.map((item) => item.toId));
}
}

Expand Down Expand Up @@ -810,7 +810,6 @@ export class ReferenceService {
return;
}

record.fields[field.id] = value;
return {
tableId,
fieldId: field.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,20 @@ export class FieldConvertingService {
private async generateReferenceFieldOps(fieldId: string) {
const topoOrdersContext = await this.fieldCalculationService.getTopoOrdersContext([fieldId]);

const { fieldMap, topoOrders, fieldId2TableId } = topoOrdersContext;
if (topoOrders.length <= 1) {
const { fieldMap, fieldId2TableId } = topoOrdersContext;

// get all fields after current field
const topoOrders = topoOrdersContext.topoOrders.slice(
topoOrdersContext.topoOrders.findIndex((item) => item.id === fieldId) + 1
);

if (!topoOrders.length) {
return {};
}

const { pushOpsMap, getOpsMap } = this.fieldOpsMap();

for (let i = 1; i < topoOrders.length; i++) {
for (let i = 0; i < topoOrders.length; i++) {
const topoOrder = topoOrders[i];
// curField will be mutate in loop
const curField = fieldMap[topoOrder.id];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ export class FieldSupplementService {
return DbFieldType.Json;
}

if (fieldType === FieldType.AutoNumber) {
return DbFieldType.Integer;
}

switch (cellValueType) {
case CellValueType.Number:
return DbFieldType.Real;
Expand Down
65 changes: 63 additions & 2 deletions apps/nestjs-backend/test/formula.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NumberFormattingType,
Relationship,
} from '@teable/core';
import { getRecord, type ITableFullVo } from '@teable/openapi';
import { getRecord, updateRecords, type ITableFullVo } from '@teable/openapi';
import {
createField,
createRecords,
Expand All @@ -16,6 +16,7 @@ import {
getRecords,
initApp,
updateRecord,
convertField,
} from './utils/init-app';

describe('OpenAPI formula (e2e)', () => {
Expand Down Expand Up @@ -181,7 +182,7 @@ describe('OpenAPI formula (e2e)', () => {
describe('safe calculate', () => {
let table: ITableFullVo;
beforeEach(async () => {
table = await createTable(baseId, { name: 'table2' });
table = await createTable(baseId, { name: 'table safe' });
});

afterEach(async () => {
Expand Down Expand Up @@ -222,5 +223,65 @@ describe('OpenAPI formula (e2e)', () => {
const record2 = await getRecord(table.id, table.records[0].id);
expect(record2.data.fields[field2.name]).toEqual(27);
});

it('should calculate auto number and number field', async () => {
const autoNumberField = await createField(table.id, {
name: 'ttttttt',
type: FieldType.AutoNumber,
});

const numberField = await createField(table.id, {
type: FieldType.Number,
});
const numberField1 = await createField(table.id, {
type: FieldType.Number,
});

await updateRecords(table.id, {
fieldKeyType: FieldKeyType.Name,
records: table.records.map((record) => ({
id: record.id,
fields: {
[numberField.name]: 2,
[numberField1.name]: 3,
},
})),
});

const formulaField = await createField(table.id, {
type: FieldType.Formula,
options: {
expression: `{${autoNumberField.id}} & "-" & {${numberField.id}} & "-" & {${numberField1.id}}`,
},
});

const record = await getRecords(table.id);
expect(record.records[0].fields[formulaField.name]).toEqual('1-2-3');
expect(record.records[0].fields[autoNumberField.name]).toEqual(1);

await convertField(table.id, formulaField.id, {
type: FieldType.Formula,
options: {
expression: `{${autoNumberField.id}} & "-" & {${numberField.id}}`,
},
});

const record2 = await getRecord(table.id, table.records[0].id);
expect(record2.data.fields[autoNumberField.name]).toEqual(1);
expect(record2.data.fields[formulaField.name]).toEqual('1-2');

await updateRecord(table.id, table.records[0].id, {
fieldKeyType: FieldKeyType.Name,
record: {
fields: {
[numberField.name]: 22,
},
},
});

const record3 = await getRecord(table.id, table.records[0].id);
expect(record3.data.fields[formulaField.name]).toEqual('1-22');
expect(record2.data.fields[autoNumberField.name]).toEqual(1);
});
});
});

0 comments on commit d5e0184

Please sign in to comment.