import 'reflect-metadata' ; import { Get , JsonController , Res , Body , Post } from 'routing-controllers' ; import { SettingService } from '../services/SettingService' ; import { Settings } from '../models/Setting' ; import { CreateSettingRequest } from './requests/CreateSettingRequest' ; import { env } from '../../env' ; import { S3Service } from '../services/S3Service' ; import { ImageService } from '../services/ImageService' ; @ JsonController ( '/settings' ) export class SettingController { constructor ( private settingService , private s3Service , private imageService ) { } api apiGroup apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/get-settings' ) public async settingsList ( @ Res ( ) response ) { const select = '' ; const relation = [ ] ; const WhereConditions = [ ] ; const limit = 1 ; const settings = await this . settingService . list ( limit , select , relation , WhereConditions ) ; const promise = settings . map ( async ( result ) => { const temp = result ; return temp ; } ) ; const value = await Promise . all ( promise ) ; const successResponse = { status : 1 , message : 'Successfully get settings' , data : value , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/create-settings' ) public async createSettings ( @ Body ( { validate : true } ) settings , @ Res ( ) response ) { console . log ( settings . metaTagKeywords ) ; const settingValue = await this . settingService . findOne ( ) ; console . log ( settingValue ) ; if ( settingValue === undefined ) { const newSettings = new Settings ( ) ; newSettings . url = settings . url ; newSettings . metaTagTitle = settings . metaTagTitle ; newSettings . metaTagDescription = settings . metaTagDescription ; newSettings . metaTagKeyword = settings . metaTagKeywords ; newSettings . storeName = settings . storeName ; newSettings . storeOwner = settings . storeOwner ; newSettings . storeAddress = settings . storeAddress ; newSettings . countryId = settings . countryId ; newSettings . zoneId = settings . zoneId ; newSettings . storeEmail = settings . storeEmail ; newSettings . storeTelephone = settings . storeTelephone ; newSettings . storeFax = settings . storeFax ; if ( settings . storeLogo ) { const logo = settings . storeLogo ; const type = logo . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const path = 'storeLogo/' ; const base64Data = new Buffer ( logo . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; if ( env . imageserver === 's3' ) { await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } newSettings . storeLogo = name ; newSettings . storeLogoPath = path ; } newSettings . maintainanceMode = settings . maintenanceMode ; newSettings . storeImage = settings . storeImage ; settingValue . invoicePrefix = settings . invoicePrefix ; settingValue . orderStatus = settings . orderStatus ; settingValue . categoryProductCount = settings . categoryProductCount ; settingValue . itemsPerPage = settings . itemsPerPage ; newSettings . google = settings . google ; newSettings . facebook = settings . facebook ; newSettings . twitter = settings . twitter ; newSettings . instagram = settings . instagram ; newSettings . isActive = settings . status ; const createdData = await this . settingService . create ( newSettings ) ; const successResponse = { status : 1 , message : 'Settings created Successfully' , data : createdData , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { settingValue . url = settings . url ; settingValue . metaTagTitle = settings . metaTagTitle ; settingValue . metaTagDescription = settings . metaTagDescription ; settingValue . metaTagKeyword = settings . metaTagKeywords ; settingValue . storeName = settings . storeName ; settingValue . storeOwner = settings . storeOwner ; settingValue . storeAddress = settings . storeAddress ; settingValue . countryId = settings . countryId ; settingValue . zoneId = settings . zoneId ; settingValue . storeEmail = settings . storeEmail ; settingValue . storeTelephone = settings . storeTelephone ; settingValue . storeFax = settings . storeFax ; settingValue . storeLogo = settings . storeLogo ; if ( settings . storeLogo ) { const logo = settings . storeLogo ; const type = logo . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const path = 'storeLogo/' ; const base64Data = new Buffer ( logo . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; if ( env . imageserver === 's3' ) { await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } settingValue . storeLogo = name ; settingValue . storeLogoPath = path ; } settingValue . maintenanceMode = settings . maintenanceMode ; settingValue . storeImage = settings . storeImage ; settingValue . invoicePrefix = settings . invoicePrefix ; settingValue . orderStatus = settings . orderStatus ; settingValue . categoryProductCount = settings . categoryProductCount ; settingValue . itemsPerPage = settings . itemsPerPage ; settingValue . google = settings . google ; settingValue . facebook = settings . facebook ; settingValue . twitter = settings . twitter ; settingValue . instagram = settings . instagram ; settingValue . isActive = settings . status ; const updatedData = await this . settingService . create ( settingValue ) ; const successResponse = { status : 1 , message : 'Settings Updated Successfully' , data : updatedData , } ; return response . status ( 200 ) . send ( successResponse ) ; } } }