import 'reflect-metadata' ; import { Get , Delete , Req , Put , QueryParam , Param , Post , Body , JsonController , Authorized , Res } from 'routing-controllers' ; import { EmailTemplate } from '../models/EmailTemplate' ; import { CreateEmailTemplate } from './requests/CreateEmailTemplateRequest' ; import { EmailTemplateService } from '../services/EmailTemplateService' ; @ JsonController ( '/email-template' ) export class EmailTemplateController { constructor ( private emailTemplateService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-email-template' ) @ Authorized ( ) public async addEmailTemplate ( @ Body ( { validate : true } ) emailTemplateParam , @ Res ( ) response ) { const emailTemplate = new EmailTemplate ( ) ; emailTemplate . title = emailTemplateParam . title ; emailTemplate . subject = emailTemplateParam . subject ; emailTemplate . content = emailTemplateParam . content ; emailTemplate . isActive = emailTemplateParam . status ; const emailTemplateSave = await this . emailTemplateService . create ( emailTemplate ) ; if ( emailTemplateSave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully created new email template.' , data : emailTemplateSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to create emailTemplate' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/email-templatelist' ) @ Authorized ( ) public async emailTemplateList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'emailTemplateId' , 'title' , 'subject' , 'content' , 'isActive' ] ; const search = [ { name : 'title' , op : 'like' , value : keyword , } , ] ; const WhereConditions = [ ] ; const emailTemplateList = await this . emailTemplateService . list ( limit , offset , select , search , WhereConditions , count ) ; if ( emailTemplateList ) { const successResponse = { status : 1 , message : 'Successfully got the complete email template list.' , data : emailTemplateList , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to list emailTemplate' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-email-template/:id' ) @ Authorized ( ) public async updateEmailTemplate ( @ Param ( 'id' ) id , @ Body ( { validate : true } ) emailTemplateParam , @ Res ( ) response ) { const emailTemplate = await this . emailTemplateService . findOne ( { where : { emailTemplateId : id , } , } ) ; if ( ! emailTemplate ) { const errorResponse = { status : 0 , message : 'Invalid emailTemplate Id' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } emailTemplate . title = emailTemplateParam . title ; emailTemplate . subject = emailTemplateParam . subject ; emailTemplate . content = emailTemplateParam . content ; emailTemplate . isActive = emailTemplateParam . status ; const templateSave = await this . emailTemplateService . create ( emailTemplate ) ; if ( templateSave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully updated the email template.' , data : templateSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to update emailTemplate' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-email-template/:id' ) @ Authorized ( ) public async deleteEmailTemplate ( @ Param ( 'id' ) id , @ Res ( ) response , @ Req ( ) request ) { const emailTemplate = await this . emailTemplateService . findOne ( { where : { emailTemplateId : id , } , } ) ; if ( ! emailTemplate ) { const errorResponse = { status : 0 , message : 'Invalid emailTemplate' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteEmailTemplate = await this . emailTemplateService . delete ( emailTemplate ) ; if ( deleteEmailTemplate ) { const successResponse = { status : 1 , message : 'Successfully deleted the email template.' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete emailTemplate' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } }