Skip to content

Commit

Permalink
add optional view_json parameter to specify custom views (#2993)
Browse files Browse the repository at this point in the history
* add optional view_json parameter to specify custom views

* don't pass view_json option to couch

* make auto view creation work for all the views
  • Loading branch information
mark2600 authored and lazedo committed Dec 16, 2016
1 parent 428bb4b commit 45a9457
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion core/kazoo_data/src/kz_datamgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,16 @@ get_results(DbName, DesignDoc) ->

get_results(DbName, DesignDoc, Options) when ?VALID_DBNAME ->
Opts = maybe_add_doc_type_from_view(DesignDoc, Options),
kzs_view:get_results(kzs_plan:plan(DbName, Opts), DbName, DesignDoc, Options);
Plan = kzs_plan:plan(DbName, Opts),

case kzs_view:get_results(Plan, DbName, DesignDoc, Options) of
{'error', 'not_found'} ->
maybe_create_view(DbName, Plan, DesignDoc, Options);

Other ->
Other
end;

get_results(DbName, DesignDoc, Options) ->
case maybe_convert_dbname(DbName) of
{'ok', Db} -> get_results(Db, DesignDoc, Options);
Expand All @@ -1111,6 +1120,17 @@ get_results_count(DbName, DesignDoc, Options) ->
Opts = maybe_add_doc_type_from_view(DesignDoc, Options),
kzs_view:get_results_count(kzs_plan:plan(DbName, Opts), DbName, DesignDoc, Options).

-spec maybe_create_view(ne_binary(), map(), ne_binary(), view_options()) -> get_results_return().
maybe_create_view(DbName, Plan, DesignDoc, Options) ->
case props:get_value('view_json', Options) of
'undefined' ->
{'error', 'not_found'};

ViewJson ->
db_view_update(DbName, ViewJson),
kzs_view:get_results(Plan, DbName, DesignDoc, Options)
end.

-spec get_result_keys(ne_binary(), ne_binary(), view_options()) ->
{'ok', ne_binaries()} | data_error().
get_result_keys(DbName, DesignDoc, Options) ->
Expand Down
2 changes: 1 addition & 1 deletion core/kazoo_modb/src/kazoo_modb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ is_account_deleted(AccountId) ->

-spec refresh_views(ne_binary()) -> 'ok'.
refresh_views(AccountMODb) ->
lager:debug("init modb ~p", [AccountMODb]),
lager:debug("refresh views on modb ~p", [AccountMODb]),
EncodedMODb = kz_util:format_account_modb(AccountMODb, 'encoded'),
Views = get_modb_views(),
_ = kapps_util:update_views(EncodedMODb, Views, 'true'),
Expand Down

0 comments on commit 45a9457

Please sign in to comment.