Files
svc-api-x/v1/resources/instance_ls.cfc
T
2024-10-30 13:46:08 +03:00

223 lines
14 KiB
Plaintext

<cfcomponent extends="taffy.core.resource" taffy:uri="/instances">
<cfsilent>
<cfimport prefix="m" taglib="../lib"/>
<cfset this.helper=CreateObject("component","lib.rest_api_helper")/><!---*** странно, почему мы его видим?---><!---вынести в апп?--->
</cfsilent>
<!---спецификация полей, пригодных для фильтрации (?и сортировки)--->
<cfset this.fieldsSpec={
dt_created={prefix="e", type="date"},
dt_updated={prefix="e", type="date"},
display_name={prefix="e", type="string"},
service_id={prefix="e", type="integer"},
svc={prefix="v", type="string"},
is_test={prefix="e", type="boolean"}
}
/>
<cffunction name="get" hint="Список экземпляров сервисов, принадлежащих данному клиенту">
<cfargument name="pageSize" type="string" hint="type:integer" default="100"/>
<cfargument name="page" type="string" hint="type:integer" default="1"/>
<cfargument name="orderBy" type="string" hint="type:string, description:comma-separated list of fields to sort by, example:fld1.ASC,fld2.DESC,fld3.ASC" default=""/>
<!---сюда можно воткнуть спецификацию полей для сортировки и фильтрации, но только ручками, идея компактного кода еще более отдаляется--->
<!---parse and validate request parameters--->
<cfset local={}/>
<cftry>
<cfset this.helper.validateField(arguments, "pageSize", "integer")/>
<cfset this.helper.validateField(arguments, "page", "integer")/>
<!---мы мирно игнорируем поля, отсутствующие в спецификации, что позволяет не делать исключения для orderBy и т.п.--->
<cfset var filter=this.helper.parseFilterParams(this.fieldsSpec)/>
<!--- <cfset var order=this.helper.parseOrderBy(this.fieldsSpec, arguments.orderBy)/> --->
<!--- <cfset var order=this.helper.parseOrderBy(this.fieldsSpec, arguments.orderBy)/> --->
<cfcatch type="invalidParamValue">
<cfreturn representationOf(this.helper.formatBadRequestError(cfcatch)).withStatus(400)/>
</cfcatch>
</cftry>
<cftry><!--- *** времянка, пока глотается ошибка 500 --->
<cfset var maxrows=arguments.pageSize*arguments.page/>
<cfset var startrow=arguments.pageSize*(arguments.page-1)+1/>
<!--- Такая бодяга. Кастомные сериализаторы не умеют вложенных структур (поразительно, или я не нашел как). Стандартный сериализатор не умеет выводить дату в ISO 8601. Приходится колхозить и форматировать на стороне БД (насилу подобрал формат). Также на стороне БД приходится конвертировать GUID, потому что драйвер pg jdbc его представляет как структуру из 2 чисел.
И то сказать, jsonb как-то странно сериализуется, в 3 поля, вместо одного Value
--->
<cfquery name="local.qRead" result="local.result">
select
<m:field_set titleMapOut="local.titleMap" lengthOut="local.fieldCount">
<m:field>e.instance_uid::text as instance_uid</m:field>
<m:field>e.display_name</m:field>
<m:field>e.service_id</m:field>
<m:field>e.specification_item_id</m:field>
<m:field>to_char(e.dt_created, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') as instance_config_dt_created</m:field><!--- to_json(e.dt_created)##>>'{}' --->
<m:field>to_char(e.dt_updated, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') as instance_config_dt_updated</m:field>
<m:field>d.contract_id</m:field>
<m:field>i.specification_id</m:field>
<m:field>s.specification</m:field>
<m:field>i.quantity</m:field>
<m:field>i.price</m:field>
<m:field>v.svc</m:field>
<m:field>(select operation from instance_state st join instance_operation io on (st.instance_operation_uid=io.instance_operation_uid) where st.instance_uid=e.instance_uid order by version desc limit 1) as operation</m:field>
<m:field>(select instance_operation_uid::text from instance_state st where st.instance_uid=e.instance_uid order by version desc limit 1) as instance_operation_uid</m:field>
<m:field>(select io.operation from instance_operation io where io.instance_uid=e.instance_uid order by dt_submit desc limit 1) as last_operation</m:field>
<m:field>(select io.instance_operation_uid::text from instance_operation io where io.instance_uid=e.instance_uid order by dt_submit desc limit 1) as last_operation_uid</m:field>
<m:field>(select io.submit_result from instance_operation io where io.instance_uid=e.instance_uid order by dt_submit desc limit 1) as submit_result</m:field>
<m:field>(select to_char(io.dt_start, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') from instance_operation io where io.instance_uid=e.instance_uid order by dt_submit desc limit 1) as operation_dt_start</m:field>
<m:field>(select to_char(io.dt_finish, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') from instance_operation io where io.instance_uid=e.instance_uid order by dt_submit desc limit 1) as operation_dt_finish</m:field>
<m:field formatter=#function(x){return (deserializeJson(x))}#>(select instance_data::text from instance_state st where st.instance_uid=e.instance_uid order by version desc limit 1) as instance_data</m:field>
<m:field>(select st.instance_state_uid::text from instance_state st where st.instance_uid=e.instance_uid order by version desc limit 1) as instance_state_uid</m:field>
<m:field>(select st.is_test from instance_state st where st.instance_uid=e.instance_uid order by version desc limit 1) as is_test</m:field>
<m:field>(select max(version) from instance_state st where st.instance_uid=e.instance_uid) as version</m:field>
</m:field_set>
from instance e
join specification_item i on (e.specification_item_id=i.specification_item_id)
join specification s on (i.specification_id=s.specification_id)
join contract d on (s.contract_id=d.contract_id)
join contragent k on (d.contragent_id=k.contragent_id)
join usr u on (k.contragent_id=u.contragent_id)
join svc v on (e.service_id=v.svc_id)
where u.usr_id=<cfqueryparam cfsqltype="cf_sql_integer" value=#arguments.usrId#/>
<m:filter_build filter=#filter#/>
order by <m:order_build sortCollection=#this.helper.parseNumericOrder(local.titleMap, arguments.orderBy)# fieldCount=0/><!---no sort length limit--->
limit #maxrows#
</cfquery>
<cfquery name="local.qTotal">
select count(*) as cnt
from instance e
join specification_item i on (e.specification_item_id=i.specification_item_id)
join specification s on (i.specification_id=s.specification_id)
join contract d on (s.contract_id=d.contract_id)
join contragent k on (d.contragent_id=k.contragent_id)
join usr u on (k.contragent_id=u.contragent_id)
join svc v on (e.service_id=v.svc_id)
where u.usr_id=<cfqueryparam cfsqltype="cf_sql_integer" value=#arguments.usrId#/>
</cfquery>
<cfset var out=structNew("linked")/>
<cfset "out.queryDurationMs"=getTickCount() - request.startTickCount/>
<cfset "out.total"=#local.qTotal.cnt#/>
<cfset var resultSet=[]/>
<cfloop query=#local.qRead# startRow=#startrow#>
<cfset var rec={}/>
<cfset this.helper.appendRecord(rec, "", local.titleMap, local.qRead, this.helper.snake2camel)/>
<cfset arrayAppend(resultSet, rec)/>
</cfloop>
<cfset "out.results"=#resultSet#/>
<cfset "out.pageSize"=#arguments.pageSize#/>
<cfset "out.page"=#arguments.page#/>
<cfset "out.orderBy"=#arguments.orderBy#/>
<cfset "out.size"=#arrayLen(resultSet)#/>
<cfset "out.runDurationMs"=getTickCount()-request.startTickCount/>
<!---<cfset "out.sql"=#local.result.sql#/>--->
<cfreturn representationOf(out)/>
<cfcatch type="any">
<cfreturn representationOf(cfcatch)/>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="post" hint="(ТЕСТИРОВАНИЕ) ВНИМАНИЕ! У текущего контрагента должна быть спецификация, в которую будет вписан создаваемый экземпляр. Новая пустая запись экземпляра. Фактическое развертывание выполняется отдельно,асинхронной операцией create сконфигурированного экземпляра. Одновременно мы создаем строку в спецификации по умолчанию и связываем экземпляр с этой строкой. Первичный ключ генерируется. Текущий пользователь, контракт, спецификация получаются из контекста.">
<cfargument name="displayName" type="string" required=true hint="type:string"/>
<cfargument name="serviceId" type="string" required=true hint="type:integer"/>
<cfargument name="descr" type="string" required=false default="" hint="type:varchar (no cleanup here!)"/>
<!--- <cfargument name="usrUid" type="string" required=true hint="type:guid"/>
<cfargument name="companyUid" type="string" required=true hint="type:guid"/> --->
<!--- оказывается, их можно и не декларировать --->
<cfset local={}/>
<cfset local.instanceUid=#createGUID()#/>
<!--- <cfdump var=#arguments#/><cfabort/> --->
<cftry>
<cfset this.helper.keyExistsAndValid(arguments, "serviceId", "integer")/>
<cfset checkDisplayName(arguments.displayName, arguments.usrId, local.instanceUid)/>
<!--- делаем вставку в спецификацию и таблицу экземпляров --->
<cfquery name="local.qSpecItem">
insert into specification_item
(specification_id, specification_item, svc_id, quantity, price, dt_created, creator_id, dt_updated, updater_id)
values
(
<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.specificationId#" />
,<cfqueryparam cfsqltype="cf_sql_varchar" value=""/>
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.serviceId#" />
,<cfqueryparam cfsqltype="cf_sql_integer" value="1" /> <!--- это типично для всех PAYG --->
,<cfqueryparam cfsqltype="cf_sql_numeric" null=true />
,<cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#" />
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.usrId#" />
,<cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#" />
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.usrId#" />
) returning specification_item_id;
</cfquery>
<cfquery name="local.qSave">
insert into instance
(instance_uid, display_name, service_id, specification_item_id, descr, dt_created, creator_id, dt_updated, updater_id)
values
(
<cfqueryparam cfsqltype="cf_sql_other" value="#local.instanceUid#" />
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.displayName#"/>
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.serviceId#" />
,<cfqueryparam cfsqltype="cf_sql_integer" value="#local.qSpecItem.specification_item_id#" />
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.descr#" />
,<cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#" />
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.usrId#" />
,<cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#" />
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.usrId#" />
);
</cfquery>
<cfcatch type="invalidParamValue">
<cfreturn representationOf(this.helper.formatBadRequestError(cfcatch)).withStatus(400)/>
</cfcatch>
</cftry>
<cfreturn noData()
.withHeaders({location: "./#local.instanceUid#"})
.withStatus(201, "Created")
/>
</cffunction>
<cffunction name="checkDisplayName">
<cfargument name="displayName" required=true/>
<cfargument name="usrId" type="numeric" required=true/>
<cfargument name="instanceUid" type="guid" required=true/>
<cfset display_name=trim(lcase(display_name))/>
<cfif len(display_name) LT 3>
<cfthrow type="InvalidParamValue" message="Instance Display Name too short" detail="Имя должно быть не короче 3 символов"/>
<cfelseif len(display_name) GT 80>
<cfthrow type="InvalidParamValue" message="Instance Display Name too long" detail="Имя должно быть не длиннее 80 символов"/>
<cfelse>
<cfquery name="qUniqueDisplayName">
select count(*) as cnt from instance e
join specification_item i on (e.specification_item_id=i.specification_item_id)
join specification s on (i.specification_id=s.specification_id)
join contract d on (s.contract_id=d.contract_id)
join contragent k on (d.contragent_id=k.contragent_id)
join usr u on (k.contragent_id=u.contragent_id)
where u.usr_id=<cfqueryparam cfsqltype="cf_sql_integer" value=#arguments.usrId#/>
AND lower(e.display_name)=lower(<cfqueryparam cfsqltype="cf_sql_varchar" value=#arguments.displayName#/>)
AND e.instance_uid <> <cfqueryparam cfsqltype="cf_sql_other" value=#arguments.instanceUid#/>
</cfquery>
<cfif qUniqueDisplayName.cnt GT 0>
<cfthrow type="InvalidParamValue" message="Instance Display Name not unique" detail="Имя экземпляра должно быть уникальным в рамках договора"/>
</cfif>
</cfif>
</cffunction>
</cfcomponent>