Skip to content

Commit

Permalink
Merge pull request #9 from radhay-samagra/master
Browse files Browse the repository at this point in the history
Fix: Search API Issue
  • Loading branch information
radhay-samagra authored Jul 22, 2022
2 parents 2483fe9 + 8af258b commit bc2a572
Show file tree
Hide file tree
Showing 39 changed files with 2,227 additions and 184 deletions.
49 changes: 49 additions & 0 deletions .history/api/src/app.module_20220722121129.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { AppService } from './app.service';
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { JsTLService } from 'src/engines/jstl/jstl.service';
import { TemplateService } from './core/template/template.service';
import { AuditService } from './core/audit/audit.service';
import { LambdaService } from './core/lambda/lambda.service';
import { LambdaController } from './core/lambda/lambda.controller';
import { TransformerService } from './core/transformer/transformer.service';
import { TransformerController } from './core/transformer/transformer.controller';
import { TemplateController } from './core/template/template.controller';
import { JinjaService } from './engines/jinja/jinja.service';
import { EjsService } from './engines/ejs/ejs.service';
import { VMService } from './core/lambda/vm.service';
import { SingletonServiceModule } from './singletonService.module';
import { I18nController } from './core/i18n/i18n/i18n.controller';
<<<<<<< HEAD
=======
import { SearchController } from './core/search/search.controller';
>>>>>>> 3296e6186e13dc977ad9c926a00ee301aac12522

@Module({
imports: [SingletonServiceModule],
controllers: [
LambdaController,
TransformerController,
TemplateController,
LambdaService,
I18nController,
<<<<<<< HEAD
=======
SearchController,
>>>>>>> 3296e6186e13dc977ad9c926a00ee301aac12522
],
exports: [VMService],
providers: [
AppService,
PrismaService,
JsTLService,
TemplateService,
AuditService,
LambdaService,
TransformerService,
JinjaService,
EjsService,
VMService,
],
})
export class AppModule {}
43 changes: 43 additions & 0 deletions .history/api/src/app.module_20220722121256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { AppService } from './app.service';
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { JsTLService } from 'src/engines/jstl/jstl.service';
import { TemplateService } from './core/template/template.service';
import { AuditService } from './core/audit/audit.service';
import { LambdaService } from './core/lambda/lambda.service';
import { LambdaController } from './core/lambda/lambda.controller';
import { TransformerService } from './core/transformer/transformer.service';
import { TransformerController } from './core/transformer/transformer.controller';
import { TemplateController } from './core/template/template.controller';
import { JinjaService } from './engines/jinja/jinja.service';
import { EjsService } from './engines/ejs/ejs.service';
import { VMService } from './core/lambda/vm.service';
import { SingletonServiceModule } from './singletonService.module';
import { I18nController } from './core/i18n/i18n/i18n.controller';
import { SearchController } from './core/search/search.controller';

@Module({
imports: [SingletonServiceModule],
controllers: [
LambdaController,
TransformerController,
TemplateController,
LambdaService,
I18nController,
SearchController,
],
exports: [VMService],
providers: [
AppService,
PrismaService,
JsTLService,
TemplateService,
AuditService,
LambdaService,
TransformerService,
JinjaService,
EjsService,
VMService,
],
})
export class AppModule {}
163 changes: 163 additions & 0 deletions .history/api/src/core/i18n/i18n/i18n.controller_20220722115508.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Param,
Post,
} from '@nestjs/common';
import { Template, TemplateType, Prisma } from '@prisma/client';
import { PrismaService } from '../../../prisma.service';
import { TransformerService } from '../../transformer/transformer.service';
import { RenderDto, RenderDtoTest, RenderResponse } from '../../dto/render';
import { JsTLService } from 'src/engines/jstl/jstl.service';
import { TemplateService } from '../../template/template.service';
import { JinjaService } from 'src/engines/jinja/jinja.service';
import { EjsService } from 'src/engines/ejs/ejs.service';

@Controller('i18n')
export class I18nController {
constructor(
private readonly jinjaService: JinjaService,
private readonly ejsService: EjsService,
private readonly prisma: PrismaService,
private readonly transformerService: TransformerService,
private readonly templateService: TemplateService,
private readonly jstlService: JsTLService,
) {}

@Post()
async addTemplate(
@Body() templateData: Prisma.TemplateCreateInput,
): Promise<Template> {
const hasDuplicate = (arrayObj, colName) => {
var hash = Object.create(null);
return arrayObj.some((arr) => {
return (
arr[colName] && (hash[arr[colName]] || !(hash[arr[colName]] = true))
);
});
};
const isDuplicate: Boolean = hasDuplicate(
templateData.bodyI18n.createMany.data,
'lang',
);
if (isDuplicate) {
throw new BadRequestException('Unique language codes not allowed');
} else {
return this.templateService.createTemplate(templateData);
}
}

@Post('process')
async render(@Body() renderDto: RenderDto): Promise<RenderResponse> {
let processed;
let template;
if (renderDto.data.lang == null) {
processed = [];
template = await this.prisma.template.findUnique({
where: { id: renderDto.id },
include: {
transformers: {
include: {
transformer: true,
},
},
bodyI18n: {
select: {
body: true,
lang: true,
},
},
},
});
const bodyArr = template.bodyI18n;
if (bodyArr.length > 0) {
bodyArr.forEach(async (element) => {
let transformedData = renderDto.data;
for (const transformer of template.transformers) {
transformedData = await this.transformerService.process(
transformer.transformer,
transformedData,
transformer.path,
);
}
let proc;
switch (template.type) {
case TemplateType.JINJA:
proc = this.jinjaService.render(element.body, transformedData);

case TemplateType.JS_TEMPLATE_LITERALS:
proc = this.jstlService.render(element.body, transformedData);
break;

case TemplateType.EJS:
proc = this.ejsService.render(element.body, transformedData);
break;
default:
throw 'Templates without template types not allowed';
}
console.log({ lang: element.lang, processed: proc });
processed.push({ lang: element.lang, processed: proc });
});
} else {
processed = {
err: 'No template found for type ' + renderDto.data.type,
};
}
} else {
template = await this.prisma.template.findUnique({
where: { id: renderDto.id },
include: {
transformers: {
include: {
transformer: true,
},
},
bodyI18n: {
where: { lang: renderDto.data.lang },
},
},
});
let transformedData = renderDto.data;
for (const transformer of template.transformers) {
transformedData = await this.transformerService.process(
transformer.transformer,
transformedData,
transformer.path,
);
}
switch (template.type) {
case TemplateType.JINJA:
processed = this.jinjaService.render(
template.bodyI18n[0].body,
transformedData,
);

case TemplateType.JS_TEMPLATE_LITERALS:
processed = this.jstlService.render(
template.bodyI18n[0].body,
transformedData,
);
break;

case TemplateType.EJS:
processed = this.ejsService.render(
template.bodyI18n[0].body,
transformedData,
);
break;
default:
throw 'Templates without template types not allowed';
}
}
return {
processed,
templateType: TemplateType.JS_TEMPLATE_LITERALS,
data: renderDto.data,
template: 'test',
meta: template.meta,
};
}
}
Loading

0 comments on commit bc2a572

Please sign in to comment.