@@ -13,21 +13,12 @@ import { templateNameSchema } from './validation';
1313import RouterDomains from '../../routerDomains/interfaces' ;
1414import { getLogger } from '../../util/logger' ;
1515import { appendDigest } from '../../util/hmac' ;
16+ import { parseJSON } from '../../common/services/json' ;
1617
1718type GetTemplateRenderedRequestParams = {
1819 name : string ;
1920} ;
2021
21- interface DomainTemplateResult {
22- template : Template | null ;
23- brandId ?: string ;
24- }
25-
26- interface DomainProps {
27- brandId ?: string ;
28- [ key : string ] : unknown ;
29- }
30-
3122const validateRequestBeforeGetTemplateRendered = validateRequestFactory ( [
3223 {
3324 schema : Joi . object ( {
@@ -37,43 +28,29 @@ const validateRequestBeforeGetTemplateRendered = validateRequestFactory([
3728 } ,
3829] ) ;
3930
40- function parseDomainProps ( props : RouterDomains [ 'props' ] ) : DomainProps {
41- if ( ! props ) {
42- return { } ;
43- }
44- if ( typeof props === 'string' ) {
45- return JSON . parse ( props ) as DomainProps ;
46- }
47- return props ;
48- }
49-
50- async function getTemplateByDomain ( domain : string , templateName : string ) : Promise < DomainTemplateResult | null > {
31+ async function getDomainByName ( domain : string ) : Promise < RouterDomains | undefined > {
5132 const [ domainItem ] = await db
5233 . select ( 'id' , 'props' )
5334 . from < RouterDomains > ( 'router_domains' )
5435 . where ( 'domainName' , String ( domain ) ) ;
5536
56- if ( ! domainItem ) {
57- return null ;
58- }
37+ return domainItem ;
38+ }
5939
40+ async function getTemplateByDomainId ( domainId : number , templateName : string ) : Promise < Template | undefined > {
6041 const [ template ] = await db
6142 . selectVersionedRowsFrom < Template > ( Tables . Templates , 'name' , EntityTypes . templates , [ `${ Tables . Templates } .*` ] )
6243 . join ( 'routes' , 'templates.name' , 'routes.templateName' )
6344 . where ( {
64- domainId : domainItem . id ,
45+ domainId,
6546 name : templateName ,
6647 } ) ;
6748
68- const props = parseDomainProps ( domainItem . props ) ;
69-
70- if ( ! template ) {
71- return { template : null , brandId : props . brandId } ;
49+ if ( template ) {
50+ template . versionId = appendDigest ( template . versionId , 'template' ) ;
7251 }
7352
74- template . versionId = appendDigest ( template . versionId , 'template' ) ;
75-
76- return { template, brandId : props . brandId } ;
53+ return template ;
7754}
7855
7956async function getTemplateByName ( templateName : string ) : Promise < Template | undefined > {
@@ -97,10 +74,11 @@ async function getRenderedTemplate(req: Request<GetTemplateRenderedRequestParams
9774 const domain = req . query . domain as string ;
9875
9976 if ( domain ) {
100- const result = await getTemplateByDomain ( domain , templateName ) ;
101- if ( result ) {
102- template = result . template ?? undefined ;
103- brandId = result . brandId ;
77+ const domainItem = await getDomainByName ( domain ) ;
78+ if ( domainItem ) {
79+ const domainProps = domainItem . props ? ( parseJSON ( domainItem . props ) as Record < string , any > ) : null ;
80+ brandId = domainProps ?. brandId ;
81+ template = await getTemplateByDomainId ( domainItem . id , templateName ) ;
10482 }
10583 }
10684
0 commit comments