Skip to content

Commit

Permalink
KAZOO-1829: if port request specifies a carrier on the root of the do…
Browse files Browse the repository at this point in the history
…c, use it

as a key to find a carrier-specific template.
  • Loading branch information
James Aimonetti authored and k-anderson committed Dec 23, 2013
1 parent be15b07 commit 8100bf2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
8 changes: 5 additions & 3 deletions applications/crossbar/src/modules/cb_loa_htmldoc.erl
Expand Up @@ -36,11 +36,13 @@ generate_loa(Context, PortRequest) ->
,{<<"qr_code">>, QRCode}
])
,ResellerId
,wh_json:get_value(<<"carrier">>, PortRequest)
).

-spec generate_loa_from_template(cb_context:context(), wh_proplist(), ne_binary()) -> cb_context:context().
generate_loa_from_template(Context, TemplateData, ResellerId) ->
Template = cb_port_requests:find_template(ResellerId),
-spec generate_loa_from_template(cb_context:context(), wh_proplist(), ne_binary(), api_binary()) ->
cb_context:context().
generate_loa_from_template(Context, TemplateData, ResellerId, Carrier) ->
Template = cb_port_requests:find_template(ResellerId, Carrier),

Renderer = wh_util:to_atom(<<ResellerId/binary, "_loa">>, 'true'),
{'ok', Renderer} = erlydtl:compile(Template, Renderer),
Expand Down
8 changes: 5 additions & 3 deletions applications/crossbar/src/modules/cb_loa_wkhtmltopdf.erl
Expand Up @@ -36,11 +36,13 @@ generate_loa(Context, PortRequest) ->
,{<<"qr_code">>, QRCode}
])
,ResellerId
,wh_json:get_value(<<"carrier">>, PortRequest)
).

-spec generate_loa_from_template(cb_context:context(), wh_proplist(), ne_binary()) -> cb_context:context().
generate_loa_from_template(Context, TemplateData, ResellerId) ->
Template = cb_port_requests:find_template(ResellerId),
-spec generate_loa_from_template(cb_context:context(), wh_proplist(), ne_binary(), api_binary()) ->
cb_context:context().
generate_loa_from_template(Context, TemplateData, ResellerId, Carrier) ->
Template = cb_port_requests:find_template(ResellerId, Carrier),

Renderer = wh_util:to_atom(<<ResellerId/binary, "_loa">>, 'true'),
{'ok', Renderer} = erlydtl:compile(Template, Renderer),
Expand Down
31 changes: 30 additions & 1 deletion applications/crossbar/src/modules/cb_port_requests.erl
Expand Up @@ -50,7 +50,7 @@
,cleanup/1

,update_default_template/0
,find_template/1
,find_template/1, find_template/2
]).

-define(MY_CONFIG_CAT, <<(?CONFIG_CAT)/binary, ".port_requests">>).
Expand Down Expand Up @@ -761,20 +761,49 @@ generate_loa_from_port(Context, PortRequest, <<"htmldoc">>) ->
cb_loa_htmldoc:generate_loa(Context, PortRequest).

-spec find_template(ne_binary()) -> ne_binary().
-spec find_template(ne_binary(), api_binary()) -> ne_binary().
find_template(ResellerId) ->
ResellerDb = wh_util:format_account_id(ResellerId, 'encoded'),
case couch_mgr:fetch_attachment(ResellerDb, ?TEMPLATE_DOC_ID, ?TEMPLATE_ATTACHMENT_ID) of
{'ok', Template} -> Template;
{'error', _} -> default_template()
end.

find_template(ResellerId, 'undefined') ->
find_template(ResellerId);
find_template(ResellerId, CarrierName) ->
CarrierTemplate = list_to_binary([?TEMPLATE_DOC_ID
,<<".">>
,wh_util:to_lower_binary(wh_util:uri_encode(CarrierName))
]),
lager:debug("looking for carrier template ~s or plain template for reseller ~s", [CarrierTemplate, ResellerId]),
ResellerDb = wh_util:format_account_id(ResellerId, 'encoded'),
case couch_mgr:fetch_attachment(ResellerDb, CarrierTemplate, ?TEMPLATE_ATTACHMENT_ID) of
{'ok', Template} -> Template;
{'error', _} -> find_carrier_template(ResellerDb, CarrierTemplate)
end.

-spec find_carrier_template(ne_binary(), ne_binary()) -> ne_binary().
find_carrier_template(ResellerDb, CarrierTemplate) ->
case couch_mgr:fetch_attachment(ResellerDb, ?TEMPLATE_DOC_ID, ?TEMPLATE_ATTACHMENT_ID) of
{'ok', Template} -> Template;
{'error', _} -> default_carrier_template(CarrierTemplate)
end.

-spec default_template() -> ne_binary().
default_template() ->
case couch_mgr:fetch_attachment(?WH_CONFIG_DB, ?TEMPLATE_DOC_ID, ?TEMPLATE_ATTACHMENT_ID) of
{'ok', Template} -> Template;
{'error', _} -> create_default_template()
end.

-spec default_carrier_template(ne_binary()) -> ne_binary().
default_carrier_template(CarrierTemplate) ->
case couch_mgr:fetch_attachment(?WH_CONFIG_DB, CarrierTemplate, ?TEMPLATE_ATTACHMENT_ID) of
{'ok', Template} -> Template;
{'error', _} -> default_template()
end.

-spec create_default_template() -> ne_binary().
create_default_template() ->
{'ok', _Doc} =
Expand Down

0 comments on commit 8100bf2

Please sign in to comment.