Skip to content

Commit

Permalink
fix: calculate field error with event and fieldKeyType (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
tea-artist authored Dec 10, 2024
1 parent b7c3b99 commit 168129f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
13 changes: 11 additions & 2 deletions apps/nestjs-backend/src/event-emitter/event-emitter.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Injectable, Logger } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import type { ICreateOpBuilder, IOpBuilder, IOpContextBase, IOtOperation } from '@teable/core';
import type {
ICreateOpBuilder,
IOpBuilder,
IOpContextBase,
IOtOperation,
IRecord,
} from '@teable/core';
import {
FieldOpBuilder,
IdPrefix,
Expand Down Expand Up @@ -262,7 +268,10 @@ export class EventEmitterService {
...existingEvent.payload,
record: {
...existingEvent.payload.record,
fields,
fields: {
...(existingEvent.payload.record as IRecord).fields,
...fields,
},
},
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Injectable } from '@nestjs/common';
import type { FieldKeyType } from '@teable/core';
import { FieldType } from '@teable/core';
import { PrismaService } from '@teable/db-main-prisma';
import { Knex } from 'knex';
Expand Down Expand Up @@ -37,6 +38,7 @@ export class SystemFieldService {

async getModifiedSystemOpsMap(
tableId: string,
fieldKeyType: FieldKeyType,
records: {
fields: Record<string, unknown>;
id: string;
Expand Down Expand Up @@ -74,13 +76,13 @@ export class SystemFieldService {

const systemRecordFields = fieldsRaw.reduce<{ [fieldId: string]: unknown }>((pre, fieldRaw) => {
const field = createFieldInstanceByRaw(fieldRaw);
const { id, type } = field;
const { type } = field;
if (type === FieldType.LastModifiedTime) {
pre[id] = timeStr;
pre[field[fieldKeyType]] = timeStr;
}

if (type === FieldType.LastModifiedBy) {
pre[id] = field.convertDBValue2CellValue({
pre[field[fieldKeyType]] = field.convertDBValue2CellValue({
id: user.id,
title: user.name,
email: user.email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class RecordOpenApiService {
},
windowId?: string
) {
const { records, order, fieldKeyType, typecast } = updateRecordsRo;
const { records, order, fieldKeyType = FieldKeyType.Name, typecast } = updateRecordsRo;
const orderIndexesBefore =
order != null && windowId
? await this.recordService.getRecordIndexes(
Expand Down Expand Up @@ -304,6 +304,7 @@ export class RecordOpenApiService {

const preparedRecords = await this.systemFieldService.getModifiedSystemOpsMap(
tableId,
fieldKeyType,
typecastRecords
);

Expand Down
18 changes: 18 additions & 0 deletions apps/nestjs-backend/test/formula.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,23 @@ describe('OpenAPI formula (e2e)', () => {
expect(record3.data.fields[formulaField.name]).toEqual('1-22');
expect(record2.data.fields[autoNumberField.name]).toEqual(1);
});

it('should update record by name wile have create last modified field', async () => {
await createField(table.id, {
type: FieldType.LastModifiedTime,
});

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

const record = await getRecord(table.id, table.records[0].id);
expect(record.data.fields[table.fields[0].name]).toEqual('1');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ export const NumberOptions = (props: {
<DefaultValue onReset={() => onDefaultValueChange(undefined)}>
<Input
type="number"
value={options?.defaultValue || ''}
onChange={(e) => onDefaultValueChange(Number(e.target.value))}
value={options?.defaultValue !== undefined ? options.defaultValue : ''}
onChange={(e) => {
const value = e.target.value;
onDefaultValueChange(value === '' ? undefined : Number(value));
}}
/>
</DefaultValue>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/common-i18n/src/locales/zh/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
"noLinkTip": "没有可查找的关联字段。请先创建关联字段,然后再次尝试配置查找。",
"fieldValidationRules": "字段值验证规则",
"enableValidateFieldUnique": "禁止重复值",
"enableValidateFieldNotNull": "禁止非空值",
"enableValidateFieldNotNull": "禁止空值",
"knowMore": "了解更多",
"linkFieldKnowMoreLink": "https://help.teable.cn/ji-ben-gong-neng/zi-duan/gao-ji-zi-duan/guan-lian",
"filterByView": "从视图筛选记录",
Expand Down

0 comments on commit 168129f

Please sign in to comment.