From f3b0064aa5d5071a8c960cedad91ab1a6798cdce Mon Sep 17 00:00:00 2001 From: root Date: Sun, 8 Nov 2020 09:59:03 +0200 Subject: [PATCH 1/2] Add temporal_route end-date --- .../callflow/src/module/cf_temporal_route.erl | 24 +++++++++++++++++-- .../callflow/src/module/cf_temporal_route.hrl | 2 ++ .../crossbar/doc/ref/temporal_rules.md | 1 + applications/crossbar/doc/temporal_rules.md | 1 + applications/crossbar/priv/api/swagger.json | 5 ++++ .../priv/couchdb/schemas/temporal_rules.json | 6 +++++ .../src/kzd_temporal_rules.erl | 13 ++++++++++ .../src/kzd_temporal_rules.erl.src | 13 ++++++++++ 8 files changed, 63 insertions(+), 2 deletions(-) diff --git a/applications/callflow/src/module/cf_temporal_route.erl b/applications/callflow/src/module/cf_temporal_route.erl index 02876fce1fa..504a9d280ab 100644 --- a/applications/callflow/src/module/cf_temporal_route.erl +++ b/applications/callflow/src/module/cf_temporal_route.erl @@ -179,17 +179,36 @@ get_temporal_rules([{Route, Id}|Routes], LSec, AccountDb, TZ, Now, Rules) -> maybe_build_rule(Routes, LSec, AccountDb, TZ, Now, Rules, Id, JObj) end. +-spec check_end_date(non_neg_integer(), kz_term:ne_binary(), kz_time:datetime(), integer(), kzd_temporal_rules:doc()) -> 'future' | 'equal' | 'past'. +check_end_date(LSec, TZ, Now, EndDateSeconds, RulesDoc) -> + % TODO, this is an workaround since the from_gregorian_seconds function are crashing on low values such as zero + case EndDateSeconds of + 0 -> 'future'; + _ -> + EndDate = kz_date:from_gregorian_seconds(kzd_temporal_rules:end_date(RulesDoc, LSec), TZ), + case kz_date:relative_difference(Now, {EndDate, {0,0,0}}) of + 'past' -> 'past'; + _ -> 'future' + end + end. + -spec maybe_build_rule(routes(), non_neg_integer(), kz_term:ne_binary(), kz_term:ne_binary(), kz_time:datetime(), rules(), kz_term:ne_binary(), kzd_temporal_rules:doc()) -> rules(). maybe_build_rule(Routes, LSec, AccountDb, TZ, Now, Rules, Id, RulesDoc) -> StartDate = kz_date:from_gregorian_seconds(kzd_temporal_rules:start_date(RulesDoc, LSec), TZ), RuleName = kzd_temporal_rules:name(RulesDoc, ?RULE_DEFAULT_NAME), - + EndDateSeconds = kzd_temporal_rules:end_date(RulesDoc), case kz_date:relative_difference(Now, {StartDate, {0,0,0}}) of 'future' -> lager:warning("rule ~p is in the future discarding", [RuleName]), get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, Rules); _ -> - get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, [build_rule(Id, RulesDoc, StartDate, RuleName) | Rules]) + case check_end_date(LSec, TZ, Now, EndDateSeconds, RulesDoc) of + 'future' -> + get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, [build_rule(Id, RulesDoc, StartDate, RuleName) | Rules]); + 'past' -> + lager:warning("rule ~p end_date is pass discarding", [RuleName]), + get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, Rules) + end end. -spec build_rule(kz_term:ne_binary(), kzd_temporal_rules:doc(), kz_time:date(), kz_term:ne_binary()) -> rule(). @@ -203,6 +222,7 @@ build_rule(Id, RulesDoc, StartDate, RuleName) -> ,name = RuleName ,ordinal = kzd_temporal_rules:ordinal(RulesDoc, ?RULE_DEFAULT_ORDINAL) ,start_date = StartDate + ,end_date = kzd_temporal_rules:end_date(RulesDoc) ,wdays = sort_wdays(kzd_temporal_rules:wdays(RulesDoc, ?RULE_DEFAULT_WDAYS)) ,wtime_start = kzd_temporal_rules:time_window_start(RulesDoc, ?RULE_DEFAULT_WTIME_START) ,wtime_stop = kzd_temporal_rules:time_window_stop(RulesDoc, ?RULE_DEFAULT_WTIME_STOP) diff --git a/applications/callflow/src/module/cf_temporal_route.hrl b/applications/callflow/src/module/cf_temporal_route.hrl index 5016d21d68c..6a690925475 100644 --- a/applications/callflow/src/module/cf_temporal_route.hrl +++ b/applications/callflow/src/module/cf_temporal_route.hrl @@ -36,6 +36,7 @@ -define(RULE_DEFAULT_ORDINAL, <<"first">>). -define(RULE_DEFAULT_MONTH, 1). -define(RULE_DEFAULT_START_DATE, {2011,1,1}). +-define(RULE_DEFAULT_END_DATE, {0,0,0}). -define(RULE_DEFAULT_WTIME_START, 0). -define(RULE_DEFAULT_WTIME_STOP, ?SECONDS_IN_DAY). @@ -51,6 +52,7 @@ ,ordinal = ?RULE_DEFAULT_ORDINAL :: ordinal() ,month = ?RULE_DEFAULT_MONTH :: kz_time:month() ,start_date = ?RULE_DEFAULT_START_DATE :: kz_time:date() + ,end_date = ?RULE_DEFAULT_END_DATE :: kz_time:date() ,wtime_start = ?RULE_DEFAULT_WTIME_START :: non_neg_integer() ,wtime_stop = ?RULE_DEFAULT_WTIME_STOP :: non_neg_integer() }). diff --git a/applications/crossbar/doc/ref/temporal_rules.md b/applications/crossbar/doc/ref/temporal_rules.md index dc935b71fce..606b0d08c5f 100644 --- a/applications/crossbar/doc/ref/temporal_rules.md +++ b/applications/crossbar/doc/ref/temporal_rules.md @@ -13,6 +13,7 @@ Key | Description | Type | Default | Required | Support Level `cycle` | The recurrence cycle for this rule | `string('date' | 'daily' | 'weekly' | 'monthly' | 'yearly')` | | `true` | `supported` `days` | The recurrence days for this rule | `array(integer())` | | `false` | `supported` `enabled` | Whether the rule is enabled | `boolean()` | | `false` | +`end_date` | The date that the rule ends. Zero means no end date | `integer()` | `0` | `false` | `supported` `flags.[]` | | `string()` | | `false` | `supported` `flags` | Flags set by external applications | `array(string())` | | `false` | `supported` `interval` | The recurrence interval for this rule | `integer()` | `1` | `false` | `supported` diff --git a/applications/crossbar/doc/temporal_rules.md b/applications/crossbar/doc/temporal_rules.md index fc141e9cc5e..4cd162061fc 100644 --- a/applications/crossbar/doc/temporal_rules.md +++ b/applications/crossbar/doc/temporal_rules.md @@ -15,6 +15,7 @@ Key | Description | Type | Default | Required | Support Level `cycle` | The recurrence cycle for this rule | `string('date' | 'daily' | 'weekly' | 'monthly' | 'yearly')` | | `true` | `supported` `days` | The recurrence days for this rule | `array(integer())` | | `false` | `supported` `enabled` | Whether the rule is enabled | `boolean()` | | `false` | +`end_date` | The date that the rule ends. Zero means no end date | `integer()` | `0` | `false` | `supported` `flags.[]` | | `string()` | | `false` | `supported` `flags` | Flags set by external applications | `array(string())` | | `false` | `supported` `interval` | The recurrence interval for this rule | `integer()` | `1` | `false` | `supported` diff --git a/applications/crossbar/priv/api/swagger.json b/applications/crossbar/priv/api/swagger.json index 75e743cf4e2..79657d976e7 100644 --- a/applications/crossbar/priv/api/swagger.json +++ b/applications/crossbar/priv/api/swagger.json @@ -36045,6 +36045,11 @@ "description": "Whether the rule is enabled", "type": "boolean" }, + "end_date": { + "default": 0, + "description": "The date that the rule ends. Zero means no end date", + "type": "integer" + }, "flags": { "description": "Flags set by external applications", "items": { diff --git a/applications/crossbar/priv/couchdb/schemas/temporal_rules.json b/applications/crossbar/priv/couchdb/schemas/temporal_rules.json index 0318260add1..983a573d34f 100644 --- a/applications/crossbar/priv/couchdb/schemas/temporal_rules.json +++ b/applications/crossbar/priv/couchdb/schemas/temporal_rules.json @@ -29,6 +29,12 @@ "description": "Whether the rule is enabled", "type": "boolean" }, + "end_date": { + "default": 0, + "description": "The date that the rule ends. Zero means no end date", + "support_level": "supported", + "type": "integer" + }, "flags": { "description": "Flags set by external applications", "items": { diff --git a/core/kazoo_documents/src/kzd_temporal_rules.erl b/core/kazoo_documents/src/kzd_temporal_rules.erl index fc6d99b5844..62e9199e4f9 100644 --- a/core/kazoo_documents/src/kzd_temporal_rules.erl +++ b/core/kazoo_documents/src/kzd_temporal_rules.erl @@ -15,6 +15,7 @@ -export([name/1, name/2, set_name/2]). -export([ordinal/1, ordinal/2, set_ordinal/2]). -export([start_date/1, start_date/2, set_start_date/2]). +-export([end_date/1, end_date/2, set_end_date/2]). -export([time_window_start/1, time_window_start/2, set_time_window_start/2]). -export([time_window_stop/1, time_window_stop/2, set_time_window_stop/2]). -export([wdays/1, wdays/2, set_wdays/2]). @@ -145,6 +146,18 @@ start_date(Doc, Default) -> set_start_date(Doc, StartDate) -> kz_json:set_value([<<"start_date">>], StartDate, Doc). +-spec end_date(doc()) -> integer(). +end_date(Doc) -> + end_date(Doc, 0). + +-spec end_date(doc(), Default) -> integer() | Default. +end_date(Doc, Default) -> + kz_json:get_integer_value([<<"end_date">>], Doc, Default). + +-spec set_end_date(doc(), integer()) -> doc(). +set_end_date(Doc, EndDate) -> + kz_json:set_value([<<"end_date">>], EndDate, Doc). + -spec time_window_start(doc()) -> integer(). time_window_start(Doc) -> time_window_start(Doc, 'undefined'). diff --git a/core/kazoo_documents/src/kzd_temporal_rules.erl.src b/core/kazoo_documents/src/kzd_temporal_rules.erl.src index 6fe41085496..677299c1aa5 100644 --- a/core/kazoo_documents/src/kzd_temporal_rules.erl.src +++ b/core/kazoo_documents/src/kzd_temporal_rules.erl.src @@ -9,6 +9,7 @@ -export([cycle/1, cycle/2, set_cycle/2]). -export([days/1, days/2, set_days/2]). -export([enabled/1, enabled/2, set_enabled/2]). +-export([end_date/1, end_date/2, set_end_date/2]). -export([flags/1, flags/2, set_flags/2]). -export([interval/1, interval/2, set_interval/2]). -export([month/1, month/2, set_month/2]). @@ -67,6 +68,18 @@ enabled(Doc, Default) -> set_enabled(Doc, Enabled) -> kz_json:set_value([<<"enabled">>], Enabled, Doc). +-spec end_date(doc()) -> integer(). +end_date(Doc) -> + end_date(Doc, 0). + +-spec end_date(doc(), Default) -> integer() | Default. +end_date(Doc, Default) -> + kz_json:get_integer_value([<<"end_date">>], Doc, Default). + +-spec set_end_date(doc(), integer()) -> doc(). +set_end_date(Doc, EndDate) -> + kz_json:set_value([<<"end_date">>], EndDate, Doc). + -spec flags(doc()) -> kz_term:api_ne_binaries(). flags(Doc) -> flags(Doc, 'undefined'). From ba05dafe1a0d74ca9464398dc86cae1d6288b1d3 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 8 Nov 2020 15:31:29 +0200 Subject: [PATCH 2/2] fix fmt --- .../callflow/src/module/cf_temporal_route.erl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/applications/callflow/src/module/cf_temporal_route.erl b/applications/callflow/src/module/cf_temporal_route.erl index 504a9d280ab..20bf0c728ea 100644 --- a/applications/callflow/src/module/cf_temporal_route.erl +++ b/applications/callflow/src/module/cf_temporal_route.erl @@ -181,14 +181,14 @@ get_temporal_rules([{Route, Id}|Routes], LSec, AccountDb, TZ, Now, Rules) -> -spec check_end_date(non_neg_integer(), kz_term:ne_binary(), kz_time:datetime(), integer(), kzd_temporal_rules:doc()) -> 'future' | 'equal' | 'past'. check_end_date(LSec, TZ, Now, EndDateSeconds, RulesDoc) -> - % TODO, this is an workaround since the from_gregorian_seconds function are crashing on low values such as zero + % TODO, this is an workaround since the from_gregorian_seconds function are crashing on low values such as zero case EndDateSeconds of 0 -> 'future'; _ -> EndDate = kz_date:from_gregorian_seconds(kzd_temporal_rules:end_date(RulesDoc, LSec), TZ), case kz_date:relative_difference(Now, {EndDate, {0,0,0}}) of - 'past' -> 'past'; - _ -> 'future' + 'past' -> 'past'; + _ -> 'future' end end. @@ -202,12 +202,12 @@ maybe_build_rule(Routes, LSec, AccountDb, TZ, Now, Rules, Id, RulesDoc) -> lager:warning("rule ~p is in the future discarding", [RuleName]), get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, Rules); _ -> - case check_end_date(LSec, TZ, Now, EndDateSeconds, RulesDoc) of - 'future' -> - get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, [build_rule(Id, RulesDoc, StartDate, RuleName) | Rules]); - 'past' -> - lager:warning("rule ~p end_date is pass discarding", [RuleName]), - get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, Rules) + case check_end_date(LSec, TZ, Now, EndDateSeconds, RulesDoc) of + 'future' -> + get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, [build_rule(Id, RulesDoc, StartDate, RuleName) | Rules]); + 'past' -> + lager:warning("rule ~p end_date is pass discarding", [RuleName]), + get_temporal_rules(Routes, LSec, AccountDb, TZ, Now, Rules) end end.