Skip to content

Commit

Permalink
feat(rules): new boolean custom type
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenn92 committed May 5, 2022
1 parent 2c3d469 commit 5a08fae
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
18 changes: 11 additions & 7 deletions server/src/modules/rules/constants/rules.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class RuleType {
RulePossibility.NOT_EQUALS,
RulePossibility.CONTAINS,
]);
static readonly BOOL = new RuleType('3', [
RulePossibility.EQUALS,
RulePossibility.NOT_EQUALS,
]);
private constructor(
private readonly key: string,
public readonly possibilities: number[],
Expand Down Expand Up @@ -96,7 +100,7 @@ export class RuleConstants {
{
id: 1,
name: 'seenBy',
humanName: 'Viewed by (list of users)',
humanName: 'Viewed by (list of usernames)',
mediaType: MediaType.MOVIE,
type: RuleType.TEXT, // returns id[]
} as Property,
Expand Down Expand Up @@ -258,9 +262,9 @@ export class RuleConstants {
{
id: 5,
name: 'monitored',
humanName: 'is monitored (1 or 0)',
humanName: 'is monitored',
mediaType: MediaType.MOVIE,
type: RuleType.NUMBER,
type: RuleType.BOOL,
} as Property,
{
id: 6,
Expand Down Expand Up @@ -364,9 +368,9 @@ export class RuleConstants {
{
id: 7,
name: 'ended',
humanName: 'Show ended (1 or 0)',
humanName: 'Show ended',
mediaType: MediaType.SHOW,
type: RuleType.NUMBER,
type: RuleType.BOOL,
} as Property,
{
id: 8,
Expand Down Expand Up @@ -427,9 +431,9 @@ export class RuleConstants {
{
id: 6,
name: 'isRequested',
humanName: 'Requested in Overseerr (1 or 0)',
humanName: 'Requested in Overseerr',
mediaType: MediaType.BOTH,
type: RuleType.NUMBER,
type: RuleType.BOOL,
} as Property,
],
},
Expand Down
5 changes: 3 additions & 2 deletions server/src/modules/rules/rule-executor.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import _, { over } from 'lodash';
import _ from 'lodash';
import { isNull } from 'lodash';
import { PlexLibraryItem } from '../api/plex-api/interfaces/library.interfaces';
import { PlexApiService } from '../api/plex-api/plex-api.service';
Expand Down Expand Up @@ -276,7 +276,8 @@ export class RuleExecutorService implements OnApplicationBootstrap {
: new Date(+rule.customVal.value * 1000)
: rule.customVal.ruleTypeId === +RuleType.TEXT
? rule.customVal.value
: rule.customVal.ruleTypeId === +RuleType.NUMBER
: rule.customVal.ruleTypeId === +RuleType.NUMBER ||
rule.customVal.ruleTypeId === +RuleType.BOOL
? +rule.customVal.value
: null;
if (
Expand Down
69 changes: 51 additions & 18 deletions ui/src/components/Rules/Rule/RuleCreator/RuleInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum RuleType {
NUMBER,
DATE,
TEXT,
BOOL,
}
enum RuleOperators {
AND,
Expand All @@ -33,6 +34,7 @@ enum CustomParams {
CUSTOM_DAYS = 'custom_days',
CUSTOM_DATE = 'custom_date',
CUSTOM_TEXT = 'custom_text',
CUSTOM_BOOLEAN = 'custom_boolean',
}

interface IRuleInput {
Expand Down Expand Up @@ -76,19 +78,24 @@ const RuleInput = (props: IRuleInput) => {
(props.editData.rule.customVal.value as number) != 0
) {
setSecondVal(CustomParams.CUSTOM_DAYS)
setRuleType(0)
setRuleType(RuleType.NUMBER)
} else {
setSecondVal(CustomParams.CUSTOM_NUMBER)
setRuleType(0)
setRuleType(RuleType.NUMBER)
}
break
case 1:
setSecondVal(CustomParams.CUSTOM_DATE)
setRuleType(1)
setRuleType(RuleType.DATE)
break
case 2:
setSecondVal(CustomParams.CUSTOM_TEXT)
setRuleType(2)
setRuleType(RuleType.TEXT)
break
case 3:
setSecondVal(CustomParams.CUSTOM_BOOLEAN)
setRuleType(RuleType.BOOL)
break
}
setCustomVal(props.editData.rule.customVal.value.toString())
} else {
Expand Down Expand Up @@ -118,7 +125,7 @@ const RuleInput = (props: IRuleInput) => {
}

const updateCustomValue = (event: { target: { value: string } }) => {
if (secondVal === 'custom_days') {
if (secondVal === CustomParams.CUSTOM_DAYS) {
setCustomVal((+event.target.value * 86400).toString())
} else {
setCustomVal(event.target.value)
Expand Down Expand Up @@ -147,10 +154,11 @@ const RuleInput = (props: IRuleInput) => {
firstval &&
action &&
((secondVal &&
secondVal !== 'custom_date' &&
secondVal !== 'custom_days' &&
secondVal !== 'custom_number' &&
secondVal !== 'custom_text') ||
secondVal !== CustomParams.CUSTOM_DATE &&
secondVal !== CustomParams.CUSTOM_DAYS &&
secondVal !== CustomParams.CUSTOM_NUMBER &&
secondVal !== CustomParams.CUSTOM_TEXT &&
secondVal !== CustomParams.CUSTOM_BOOLEAN) ||
customVal)
) {
const ruleValues = {
Expand All @@ -167,10 +175,13 @@ const RuleInput = (props: IRuleInput) => {
? customValType
: customValType === RuleType.NUMBER
? customValType
: customValType === RuleType.TEXT && secondVal === 'custom_days'
: customValType === RuleType.TEXT &&
secondVal === CustomParams.CUSTOM_DAYS
? RuleType.NUMBER
: customValType === RuleType.TEXT
? customValType
: customValType === RuleType.BOOL
? customValType
: +ruleType
: +ruleType,
value: customVal,
Expand Down Expand Up @@ -205,18 +216,21 @@ const RuleInput = (props: IRuleInput) => {

useEffect(() => {
if (secondVal) {
if (secondVal === 'custom_number') {
if (secondVal === CustomParams.CUSTOM_NUMBER) {
setCustomValActive(true)
setCustomValType(RuleType.NUMBER)
} else if (secondVal === 'custom_date') {
} else if (secondVal === CustomParams.CUSTOM_DATE) {
setCustomValActive(true)
setCustomValType(RuleType.DATE)
} else if (secondVal === 'custom_days') {
} else if (secondVal === CustomParams.CUSTOM_DAYS) {
setCustomValActive(true)
setCustomValType(RuleType.TEXT)
} else if (secondVal === 'custom_text') {
} else if (secondVal === CustomParams.CUSTOM_TEXT) {
setCustomValActive(true)
setCustomValType(RuleType.TEXT)
} else if (secondVal === CustomParams.CUSTOM_BOOLEAN) {
setCustomValActive(true)
setCustomValType(RuleType.BOOL)
} else {
setCustomValActive(false)
setCustomVal(undefined)
Expand Down Expand Up @@ -390,20 +404,27 @@ const RuleInput = (props: IRuleInput) => {
<optgroup label={`Custom value's`}>
{ruleType === RuleType.DATE ? (
<>
<option value="custom_days">Amount of days.. </option>
<option value={CustomParams.CUSTOM_DAYS}>
Amount of days
</option>
{action &&
+action !== +RulePossibility.IN_LAST &&
action &&
+action !== +RulePossibility.IN_NEXT ? (
<option value="custom_date">Specific date.. </option>
<option value={CustomParams.CUSTOM_DATE}>
Specific date
</option>
) : undefined}
</>
) : undefined}
{ruleType === RuleType.NUMBER ? (
<option value="custom_number">Custom number.. </option>
<option value={CustomParams.CUSTOM_NUMBER}>Number</option>
) : undefined}
{ruleType === RuleType.BOOL ? (
<option value={CustomParams.CUSTOM_BOOLEAN}>Boolean</option>
) : undefined}
{ruleType === RuleType.TEXT ? (
<option value="custom_text">Custom text.. </option>
<option value={CustomParams.CUSTOM_TEXT}>Text</option>
) : undefined}
</optgroup>
{ConstantsCtx.constants.applications?.map((app) => {
Expand Down Expand Up @@ -469,6 +490,18 @@ const RuleInput = (props: IRuleInput) => {
value={customVal}
placeholder="Date"
></input>
) : customValType === RuleType.BOOL ? (
<select
name="custom_val"
id="custom_val"
onChange={updateCustomValue}
value={customVal}
>
<option defaultChecked value={1}>
True
</option>
<option value={0}>False</option>
</select>
) : (
<input
type="number"
Expand Down

0 comments on commit 5a08fae

Please sign in to comment.