-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
194 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/command-handlers/src/handlers/trigger-record-button.command-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TriggerRecordButtonCommand } from "@undb/commands" | ||
import { commandHandler } from "@undb/cqrs" | ||
import { singleton } from "@undb/di" | ||
import { type ICommandHandler } from "@undb/domain" | ||
import { injectRecordsService, type IRecordsService } from "@undb/table" | ||
|
||
@commandHandler(TriggerRecordButtonCommand) | ||
@singleton() | ||
export class TriggerRecordButtonCommandHandler implements ICommandHandler<TriggerRecordButtonCommand, any> { | ||
constructor( | ||
@injectRecordsService() | ||
private readonly service: IRecordsService, | ||
) {} | ||
|
||
async execute(command: TriggerRecordButtonCommand): Promise<any> { | ||
return this.service.triggerRecordButton(command, { recordId: command.recordId, field: command.field }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Command, type CommandProps } from "@undb/domain" | ||
import { fieldId, fieldName, recordId, uniqueTableDTO } from "@undb/table" | ||
import { z } from "@undb/zod" | ||
|
||
export const triggerRecordButtonCommand = z | ||
.object({ | ||
recordId, | ||
field: fieldId.or(fieldName), | ||
}) | ||
.merge(uniqueTableDTO) | ||
|
||
export type ITriggerRecordButtonCommand = z.infer<typeof triggerRecordButtonCommand> | ||
|
||
export class TriggerRecordButtonCommand extends Command implements ITriggerRecordButtonCommand { | ||
public readonly recordId: string | ||
public readonly field: string | ||
public readonly tableId?: string | ||
public readonly tableName?: string | ||
public readonly baseName?: string | ||
|
||
constructor(props: CommandProps<ITriggerRecordButtonCommand>) { | ||
super(props) | ||
this.recordId = props.recordId | ||
this.field = props.field | ||
this.tableId = props.tableId | ||
this.baseName = props.baseName | ||
this.tableName = props.tableName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/table/src/modules/records/record/dto/trigger-record-button.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from "@undb/zod" | ||
import { fieldId, fieldName } from "../../../schema" | ||
import { recordId } from "../record-id.vo" | ||
|
||
export const triggerRecordButtonDTO = z.object({ | ||
recordId, | ||
field: fieldId.or(fieldName), | ||
}) | ||
|
||
export type ITriggerRecordButtonDTO = z.infer<typeof triggerRecordButtonDTO> |
43 changes: 43 additions & 0 deletions
43
packages/table/src/modules/records/services/methods/trigger-record-button.method.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { None, Option, Some } from "@undb/domain" | ||
import { objectify } from "radash" | ||
import type { IUniqueTableDTO } from "../../../../dto" | ||
import { withUniqueTable } from "../../../../specifications" | ||
import { RecordDO, RecordIdVO } from "../../record" | ||
import type { ITriggerRecordButtonDTO } from "../../record/dto/trigger-record-button.dto" | ||
import type { RecordsService } from "../records.service" | ||
|
||
export async function triggerRecordButtonMethod( | ||
this: RecordsService, | ||
t: IUniqueTableDTO, | ||
dto: ITriggerRecordButtonDTO, | ||
): Promise<Option<RecordDO>> { | ||
const ts = withUniqueTable(t).unwrap() | ||
const table = (await this.tableRepository.findOne(Some(ts))).expect("Table not found") | ||
|
||
const field = table.schema.getFieldByIdOrName(dto.field).expect("Field not found") | ||
if (field.type !== "button") { | ||
throw new Error("Field is not a button") | ||
} | ||
|
||
const option = field.option.into(undefined) | ||
if (!option) return None | ||
const action = option.action | ||
if (!action.values.length) return None | ||
|
||
const record = (await this.repo.findOneById(table, new RecordIdVO(dto.recordId))).expect( | ||
"Record not found: " + dto.recordId, | ||
) | ||
|
||
const disabled = field.getIsDisabled(table, record) | ||
if (disabled) return None | ||
|
||
const values = objectify( | ||
action.values, | ||
(v) => v.field!, | ||
(v) => v.value ?? null, | ||
) | ||
const spec = record.update(table, values) | ||
await this.repo.updateOneById(table, record, spec) | ||
|
||
return Some(record) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters