Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

5.0 KB
Raw Blame
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Expose, plainToInstance } from 'class-transformer';
import { IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString, Max, MaxLength, Min, ValidateIf } from 'class-validator';
import { ToStrictBoolean, ToStrictNumber, ToStrictString } from '../../../common/utils/strict-boolean';
import { MaxCodePoints } from '../../../common/validation/max-code-points';
import { NoNulCharacter } from '../../../common/validation/no-nul-character';
import { MESSAGE_TEXT_MAX_LENGTH } from '../../message/dto/send-message.dto';
import { WebhookFilters } from '../../webhook/filters/filter-types';
import { IsValidWebhookFilters } from '../../webhook/filters/filter-validation';
import { AutomationRule } from '../entities/automation-rule.entity';

/** Longest quiet period a rule may ask for: one day. */
export const AUTOMATION_COOLDOWN_MAX_SECONDS = 86_400;

const CONDITIONS_DESCRIPTION =
  'Match conditions in the webhook filter format (message family: sender, recipient, chatId, body, ' +
  'type, isGroup, kind, fromMe, hasMedia, mentions). All conditions must match (AND). Omitted or ' +
  'empty means the rule matches every inbound message except channel, broadcast-list and status ' +
  'messages, which a rule answers only when it has a `kind` condition that matches them. On Baileys ' +
  "a message received through a contact's broadcast list is filed under the sender's chat (`kind` " +
  'individual), so it matches.';

const COOLDOWN_DESCRIPTION =
  'Quiet period per chat, in seconds: after the rule replies in a chat it stays silent there for ' +
  'this long (default 60, 0 disables). This is the guard against two auto-repliers answering each ' +
  'other forever, so disable it knowingly.';

export class CreateAutomationRuleDto {
  @ApiProperty({ description: 'Display name for the rule', example: 'Greet new enquiries', maxLength: 100 })
  @ToStrictString()
  @IsString()
  @IsNotEmpty()
  @MaxCodePoints(100)
  @NoNulCharacter()
  name!: string;

  @ApiProperty({
    description: 'Text sent back into the chat when the rule matches',
    example: 'Thanks for reaching out — we reply within the hour.',
    maxLength: MESSAGE_TEXT_MAX_LENGTH,
  })
  @ToStrictString()
  @IsString()
  @IsNotEmpty()
  @MaxLength(MESSAGE_TEXT_MAX_LENGTH)
  @NoNulCharacter()
  replyText!: string;

  @ApiPropertyOptional({ description: CONDITIONS_DESCRIPTION })
  @IsOptional()
  @IsValidWebhookFilters()
  conditions?: WebhookFilters | null;

  @ApiPropertyOptional({
    description: COOLDOWN_DESCRIPTION,
    default: 60,
    minimum: 0,
    maximum: AUTOMATION_COOLDOWN_MAX_SECONDS,
  })
  @IsOptional()
  @ToStrictNumber()
  @IsInt()
  @Min(0)
  @Max(AUTOMATION_COOLDOWN_MAX_SECONDS)
  cooldownSeconds?: number;

  @ApiPropertyOptional({ description: 'Whether the rule is active', default: true })
  @IsOptional()
  @ToStrictBoolean()
  @IsBoolean()
  enabled?: boolean;
}

export class UpdateAutomationRuleDto {
  @ApiPropertyOptional({ description: 'Display name for the rule', maxLength: 100 })
  // Not @IsOptional on the NOT NULL columns: that also skips null, which then reaches save() as a 500.
  @ValidateIf((o: UpdateAutomationRuleDto) => o.name !== undefined)
  @ToStrictString()
  @IsString()
  @IsNotEmpty()
  @MaxCodePoints(100)
  @NoNulCharacter()
  name?: string;

  @ApiPropertyOptional({
    description: 'Text sent back into the chat when the rule matches',
    maxLength: MESSAGE_TEXT_MAX_LENGTH,
  })
  @ValidateIf((o: UpdateAutomationRuleDto) => o.replyText !== undefined)
  @ToStrictString()
  @IsString()
  @IsNotEmpty()
  @MaxLength(MESSAGE_TEXT_MAX_LENGTH)
  @NoNulCharacter()
  replyText?: string;

  @ApiPropertyOptional({ description: CONDITIONS_DESCRIPTION })
  @IsOptional()
  @IsValidWebhookFilters()
  conditions?: WebhookFilters | null;

  @ApiPropertyOptional({ description: COOLDOWN_DESCRIPTION, minimum: 0, maximum: AUTOMATION_COOLDOWN_MAX_SECONDS })
  @ValidateIf((o: UpdateAutomationRuleDto) => o.cooldownSeconds !== undefined)
  @ToStrictNumber()
  @IsInt()
  @Min(0)
  @Max(AUTOMATION_COOLDOWN_MAX_SECONDS)
  cooldownSeconds?: number;

  @ApiPropertyOptional({ description: 'Whether the rule is active' })
  @ValidateIf((o: UpdateAutomationRuleDto) => o.enabled !== undefined)
  @ToStrictBoolean()
  @IsBoolean()
  enabled?: boolean;
}

export class AutomationRuleResponseDto {
  @ApiProperty()
  @Expose()
  id!: string;

  @ApiProperty()
  @Expose()
  sessionId!: string;

  @ApiProperty()
  @Expose()
  name!: string;

  @ApiProperty()
  @Expose()
  enabled!: boolean;

  @ApiPropertyOptional({ description: CONDITIONS_DESCRIPTION, nullable: true })
  @Expose()
  conditions!: WebhookFilters | null;

  @ApiProperty()
  @Expose()
  replyText!: string;

  @ApiProperty()
  @Expose()
  cooldownSeconds!: number;

  @ApiProperty()
  @Expose()
  createdAt!: Date;

  @ApiProperty()
  @Expose()
  updatedAt!: Date;

  static fromEntity(rule: AutomationRule): AutomationRuleResponseDto {
    return plainToInstance(AutomationRuleResponseDto, rule, { excludeExtraneousValues: true });
  }
}