// @ts-ignore import {SiteClient} from 'datocms-client'; import {TYPE} from './constants'; const DEFAULT_COLOUR_ID = '684464'; const WORK_SANS_FONT_ID = '677697'; const DEFAULT_IMAGE = '520606'; const DEFAULT_LOGO = '527157'; const DEFAULT_LOGO_ICON = '527157'; export class RecordsApi { projectId: string; name: string; client: any; constructor(projectId: string, token: any) { console.log('RecordsApi() validator projectId=', projectId, 'token=', token); this.projectId = projectId; this.name = projectId[0].toUpperCase() + projectId.slice(1); this.client = SiteClient(token); } async create(tag: string, itemType: string, data: any): Promise { console.log(`create::${tag}`, itemType); const item = await this.client.items.create({ itemType, ...data }); return this.client.items.publish(item.id); } async generate(): Promise { const merchant = await this.createMerchant(); const merchantId = merchant.id; const homePage = await this.createHomePage(merchantId); // TODo abstract the following 3 const homePageMenuItem = await this.create('menuitem', TYPE.homepageMenuItem, { title: 'Home', page: homePage.id, merchant: merchantId }); const shopMenuItem = await this.create('shopmenuitem', TYPE.shopMenuItem, { title: 'Shop', merchant: merchantId }); const socialItem = await this.create('socialItem', TYPE.socialMenuItem, { platform: 'instagram', url: 'https://instagram.com' }); await this.createMerchantTheme(merchantId); await this.createHeaderLayout( merchantId, homePageMenuItem.id, shopMenuItem.id ); await this.createFooterLayout( merchantId, homePageMenuItem.id, shopMenuItem.id ); await this.createSiteConfig( merchantId, socialItem.id ); return 'generate::Done'; } async createMerchant(): Promise { console.log('createMerchant'); return this.create('merchant', TYPE.merchant, { floomxMerchantTitle: this.name, title: this.name, location: { latitude: 0, longitude: 0 }, projectId: this.projectId, pages: null }); } async createGlobalButton(merchantId: string): Promise { return this.create('globalButton', TYPE.globalButton, { cmsTitle: this.name, roundedness: 'Not Rounded', merchant: merchantId, idleBackground: DEFAULT_COLOUR_ID, idleTextColour: DEFAULT_COLOUR_ID, hoverBackground: DEFAULT_COLOUR_ID, hoverTextColour: DEFAULT_COLOUR_ID }); } async createHeaderLayout(merchantId: string, homePageMenuItemId: string, shopMenuItemId: string): Promise { return this.create('headerLayout', TYPE.headerLayout, { cmsTitle: this.name, merchant: merchantId, layoutOption: 'Top Thin', backgroundColour: DEFAULT_COLOUR_ID, textColour: DEFAULT_COLOUR_ID, textHighlightColour: DEFAULT_COLOUR_ID, iconColour: DEFAULT_COLOUR_ID, highlightColour: DEFAULT_COLOUR_ID, useHeadingFont: true, menuItems: [ homePageMenuItemId, shopMenuItemId ], showSocial: true }); } async createFooterLayout(merchantId: string, homePageMenuItemId: string, shopMenuItemId: string): Promise { return this.create('footerLayout', TYPE.footerLayout, { cmsTitle: this.name, merchant: merchantId, layoutOption: 'Centered', backgroundColour: DEFAULT_COLOUR_ID, textColour: DEFAULT_COLOUR_ID, iconColour: DEFAULT_COLOUR_ID, highlightColour: DEFAULT_COLOUR_ID, useHeadingFont: true, menuItems: [ homePageMenuItemId, shopMenuItemId ], showSocial: true }); } async createMerchantTheme(merchantId: string): Promise { const globalButton = await this.createGlobalButton(merchantId); return this.create('merchantTheme', TYPE.themeConfiguration, { cmsTitle: this.name, merchant: merchantId, primary: { red: 255, blue: 0, alpha: 255, green: 0 }, primaryLight: { red: 255, blue: 147, alpha: 255, green: 147 }, secondary: { red: 28, blue: 255, alpha: 255, green: 0 }, accent: { red: 0, blue: 0, alpha: 255, green: 255 }, backgroundDark: { red: 0, blue: 0, alpha: 255, green: 0 }, backgroundLight: { red: 255, blue: 255, alpha: 255, green: 255 }, textLight: { red: 255, blue: 255, alpha: 255, green: 255 }, textMedium: { red: 132, blue: 132, alpha: 255, green: 132 }, textDark: { red: 0, blue: 0, alpha: 255, green: 0 }, bodyFontOption: WORK_SANS_FONT_ID, headingFontOption: WORK_SANS_FONT_ID, globalHeadingTextColour: DEFAULT_COLOUR_ID, buttonConfiguration: globalButton.id, heroBackgroundColour: DEFAULT_COLOUR_ID, heroTitleTextColour: DEFAULT_COLOUR_ID, heroBodyTextColour: DEFAULT_COLOUR_ID, heroBackgroundOpacity: 0.9, newsletterBackgroundTint: DEFAULT_COLOUR_ID, contactModuleBackgroundTint: DEFAULT_COLOUR_ID, productItemTextAlignment: 'Center', siteBackgroundColour: DEFAULT_COLOUR_ID, containerWidth: 'Medium', textAndImageHeadingColour: DEFAULT_COLOUR_ID, textAndImageBodyTextColour: DEFAULT_COLOUR_ID, textAndImageBackgroundColour: DEFAULT_COLOUR_ID, categoryModuleActiveBg: DEFAULT_COLOUR_ID, categoryModuleCategoryBg: DEFAULT_COLOUR_ID, categoryModuleTextColour: DEFAULT_COLOUR_ID, categoryModuleActiveTextColour: DEFAULT_COLOUR_ID, categoryModuleBackgroundColour: DEFAULT_COLOUR_ID }); } async createHomePage(merchantId: string): Promise { const headerContentModule = await this.create('headerContentModule', TYPE.headerConfiguration, { text: 'Home page', isLink: false, layoutOption: 'Centered', externalUrl: null, internalPage: null }); return this.create('HomePage', TYPE.contentPage, { title: `${this.projectId} - Home`, slug: 'index', previewLink: null, merchant: merchantId, seoMeta: { image: null, title: 'Home', description: 'Home' }, content: [headerContentModule.id] }); } async createSiteConfig(merchantId: string, socialItemId: string): Promise { return this.create('SiteConfig', TYPE.siteConfig, { cmsTitle: this.name, merchant: merchantId, metaTags: { image: DEFAULT_IMAGE, title: this.name, description: 'Description' }, logo: DEFAULT_LOGO, logoIcon: DEFAULT_LOGO, socialMedia: [ socialItemId ], contactEmail: null, contactPhoneNumber: null }); } } exports.RecordsApi = RecordsApi;