|
| 1 | +package com.nylas.models |
| 2 | + |
| 3 | +import com.squareup.moshi.Json |
| 4 | + |
| 5 | +/** |
| 6 | + * Class representation of a Nylas create rule request. |
| 7 | + */ |
| 8 | +data class CreateRuleRequest( |
| 9 | + /** |
| 10 | + * Name of the rule. |
| 11 | + */ |
| 12 | + @Json(name = "name") |
| 13 | + val name: String, |
| 14 | + /** |
| 15 | + * When this rule is evaluated. |
| 16 | + */ |
| 17 | + @Json(name = "trigger") |
| 18 | + val trigger: RuleTrigger, |
| 19 | + /** |
| 20 | + * The match conditions for this rule. |
| 21 | + */ |
| 22 | + @Json(name = "match") |
| 23 | + val match: RuleMatch, |
| 24 | + /** |
| 25 | + * The actions to perform when conditions are met. |
| 26 | + */ |
| 27 | + @Json(name = "actions") |
| 28 | + val actions: List<RuleAction>, |
| 29 | + /** |
| 30 | + * Optional description of the rule. |
| 31 | + */ |
| 32 | + @Json(name = "description") |
| 33 | + val description: String? = null, |
| 34 | + /** |
| 35 | + * Evaluation order — lower numbers run first. Range 0–1000, default 10. |
| 36 | + */ |
| 37 | + @Json(name = "priority") |
| 38 | + val priority: Int? = null, |
| 39 | + /** |
| 40 | + * Whether the rule is active. Defaults to true. |
| 41 | + */ |
| 42 | + @Json(name = "enabled") |
| 43 | + val enabled: Boolean? = null, |
| 44 | +) { |
| 45 | + init { |
| 46 | + require(!(actions.any { it.type == RuleActionType.BLOCK } && actions.size > 1)) { |
| 47 | + "RuleActionType.BLOCK is terminal and cannot be combined with other actions." |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Builder for [CreateRuleRequest]. |
| 53 | + * @param name Name of the rule. |
| 54 | + * @param trigger When this rule is evaluated. |
| 55 | + * @param match The match conditions. |
| 56 | + * @param actions The actions to perform. |
| 57 | + */ |
| 58 | + class Builder( |
| 59 | + private val name: String, |
| 60 | + private val trigger: RuleTrigger, |
| 61 | + private val match: RuleMatch, |
| 62 | + private val actions: List<RuleAction>, |
| 63 | + ) { |
| 64 | + private var description: String? = null |
| 65 | + private var priority: Int? = null |
| 66 | + private var enabled: Boolean? = null |
| 67 | + |
| 68 | + /** |
| 69 | + * Set the description of the rule. |
| 70 | + * @param description Optional description of the rule. |
| 71 | + * @return The builder. |
| 72 | + */ |
| 73 | + fun description(description: String) = apply { this.description = description } |
| 74 | + |
| 75 | + /** |
| 76 | + * Set the evaluation priority. |
| 77 | + * @param priority Evaluation order — lower numbers run first. Range 0–1000. |
| 78 | + * @return The builder. |
| 79 | + */ |
| 80 | + fun priority(priority: Int) = apply { this.priority = priority } |
| 81 | + |
| 82 | + /** |
| 83 | + * Set whether the rule is active. |
| 84 | + * @param enabled Whether the rule is active. |
| 85 | + * @return The builder. |
| 86 | + */ |
| 87 | + fun enabled(enabled: Boolean) = apply { this.enabled = enabled } |
| 88 | + |
| 89 | + /** |
| 90 | + * Build the [CreateRuleRequest]. |
| 91 | + * @return A [CreateRuleRequest] with the provided values. |
| 92 | + * @throws IllegalArgumentException if [RuleActionType.BLOCK] is combined with other actions. |
| 93 | + */ |
| 94 | + fun build() = CreateRuleRequest(name, trigger, match, actions, description, priority, enabled) |
| 95 | + } |
| 96 | +} |
0 commit comments