@@ -13,6 +13,7 @@ import { templateNameSchema } from './validation';
1313import RouterDomains from '../../routerDomains/interfaces' ;
1414import { getLogger } from '../../util/logger' ;
1515import { appendDigest } from '../../util/hmac' ;
16+ import { JSONValue , parseJSON } from '../../common/services/json' ;
1617
1718type GetTemplateRenderedRequestParams = {
1819 name : string ;
@@ -27,21 +28,21 @@ const validateRequestBeforeGetTemplateRendered = validateRequestFactory([
2728 } ,
2829] ) ;
2930
30- async function getTemplateByDomain ( domain : string , templateName : string ) : Promise < Template | null > {
31+ async function getDomainByName ( domain : string ) : Promise < RouterDomains | undefined > {
3132 const [ domainItem ] = await db
32- . select ( 'id' )
33+ . select ( 'id' , 'props' )
3334 . from < RouterDomains > ( 'router_domains' )
3435 . where ( 'domainName' , String ( domain ) ) ;
3536
36- if ( ! domainItem ) {
37- return null ;
38- }
37+ return domainItem ;
38+ }
3939
40+ async function getTemplateByDomainId ( domainId : number , templateName : string ) : Promise < Template | undefined > {
4041 const [ template ] = await db
4142 . selectVersionedRowsFrom < Template > ( Tables . Templates , 'name' , EntityTypes . templates , [ `${ Tables . Templates } .*` ] )
4243 . join ( 'routes' , 'templates.name' , 'routes.templateName' )
4344 . where ( {
44- domainId : domainItem . id ,
45+ domainId,
4546 name : templateName ,
4647 } ) ;
4748
@@ -65,19 +66,27 @@ async function getTemplateByName(templateName: string): Promise<Template | undef
6566}
6667
6768async function getRenderedTemplate ( req : Request < GetTemplateRenderedRequestParams > , res : Response ) : Promise < void > {
68- let template ;
69+ let template : Template | undefined ;
70+ let brandId : string | undefined ;
6971
7072 const { name : templateName } = req . params ;
71-
72- const { locale , domain } = req . query ;
73+ const locale = req . query . locale as string ;
74+ const domain = req . query . domain as string ;
7375
7476 if ( domain ) {
75- template = await getTemplateByDomain ( String ( domain ) , templateName ) ;
77+ const domainItem = await getDomainByName ( domain ) ;
78+ if ( domainItem ) {
79+ const domainProps = domainItem . props ? parseJSON < Record < string , JSONValue > > ( domainItem . props ) : null ;
80+ brandId = typeof domainProps ?. brandId === 'string' ? domainProps . brandId : undefined ;
81+ template = await getTemplateByDomainId ( domainItem . id , templateName ) ;
82+ }
7683 }
7784
7885 if ( ! template ) {
7986 template = await getTemplateByName ( templateName ) ;
80- template && getLogger ( ) . info ( `Template ${ templateName } is not attached to the domain, found by template name.` ) ;
87+ if ( template ) {
88+ getLogger ( ) . info ( `Template ${ templateName } is not attached to the domain, found by template name.` ) ;
89+ }
8190 }
8291
8392 if ( ! template ) {
@@ -91,15 +100,15 @@ async function getRenderedTemplate(req: Request<GetTemplateRenderedRequestParams
91100 . select ( )
92101 . from < LocalizedTemplateRow > ( Tables . TemplatesLocalized )
93102 . where ( 'templateName' , templateName )
94- . andWhere ( 'locale' , locale as string ) ;
103+ . andWhere ( 'locale' , locale ) ;
95104
96105 if ( localizedTemplate ) {
97106 content = localizedTemplate . content ;
98107 }
99108 }
100109
101110 try {
102- const renderedTemplate = await renderTemplate ( content ) ;
111+ const renderedTemplate = await renderTemplate ( content , brandId ? { brandId } : undefined ) ;
103112 res . status ( 200 ) . send ( { ...template , ...renderedTemplate } ) ;
104113 } catch ( e ) {
105114 if ( e instanceof errors . FetchIncludeError ) {
0 commit comments