Skip to content

Commit

Permalink
wip-handoff
Browse files Browse the repository at this point in the history
  • Loading branch information
AinaVendrell committed Jun 15, 2021
1 parent 764fd43 commit 1fc4b00
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 57 deletions.
2 changes: 2 additions & 0 deletions packages/botonic-plugin-contentful/src/cms/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export class TopContentId extends ContentId {
return cms.url(this.id, context)
case ContentType.SCHEDULE:
return cms.schedule(this.id)
case ContentType.HANDOFF:
return cms.handoff(this.id, context)
default:
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Expand Down
13 changes: 13 additions & 0 deletions packages/botonic-plugin-contentful/src/cms/cms-dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
DateRangeContent,
Document,
Element,
Handoff,
HandoffQueue,
Image,
Queue,
ScheduleContent,
Expand Down Expand Up @@ -63,6 +65,17 @@ export class DummyCMS implements CMS {
)
}

async handoff(id: string, {} = DEFAULT_CONTEXT): Promise<Handoff> {
return Promise.resolve(
new Handoff(
new CommonFields(id, id),
'Dummy text for ' + id,
this.buttonCallbacks[0],
new HandoffQueue('queue')
)
)
}

chitchat(id: string, context = DEFAULT_CONTEXT): Promise<Chitchat> {
return this.text(id, context)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/botonic-plugin-contentful/src/cms/cms-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DateRangeContent,
Document,
Element,
Handoff,
Image,
Queue,
ScheduleContent,
Expand Down Expand Up @@ -80,6 +81,11 @@ export class LogCMS implements CMS {
return this.cms.element(id, context)
}

handoff(id: string, context?: Context): Promise<Handoff> {
this.logContentDelivery(ContentType.HANDOFF, id, context)
return this.cms.handoff(id, context)
}

content(id: string, context?: Context): Promise<Content> {
this.logContentDelivery('content' as ContentType, id, context)
return this.cms.content(id, context)
Expand Down
5 changes: 5 additions & 0 deletions packages/botonic-plugin-contentful/src/cms/cms-multilocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DateRangeContent,
Document,
Element,
Handoff,
Image,
Queue,
ScheduleContent,
Expand Down Expand Up @@ -95,6 +96,10 @@ export class MultiContextCms implements CMS {
return this.cmsFromContext(context).text(id, context)
}

handoff(id: string, context?: Context): Promise<Handoff> {
return this.cmsFromContext(context).handoff(id, context)
}

topContents<T extends TopContent>(
model: TopContentType,
context?: Context,
Expand Down
4 changes: 2 additions & 2 deletions packages/botonic-plugin-contentful/src/cms/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export class OnFinishPath {
constructor(readonly path: string) {}
}

export type OnFinish = OnFinishPath | OnFinishPayload
export type OnFinish = Callback

export class HandoffAgentEmail {
readonly type = 'AGENT_EMAIL'
Expand Down Expand Up @@ -472,7 +472,7 @@ export class Handoff extends TopContent {
constructor(
readonly common: CommonFields,
readonly text: string,
readonly onFinish: OnFinish,
readonly onFinish?: OnFinish,
readonly destination?: HandoffDestination,
readonly shadowing?: boolean
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
Document,
Element,
FollowUp,
Handoff,
HandoffDestination,
Image,
OnFinish,
StartUp,
Text,
} from '../contents'
Expand Down Expand Up @@ -219,3 +222,37 @@ export class DocumentBuilder extends MessageContentBuilder {
return new Document(this.buildCommonFields(), this.docUrl)
}
}

export class HandoffBuilder extends MessageContentBuilder {
onFinish?: OnFinish
destination?: HandoffDestination
shadowing?: boolean
constructor(id: string, name: string, public text: string) {
super(id, name)
}

withOnFinish(onFinish?: OnFinish): this {
this.onFinish = onFinish
return this
}

withDestination(destination: HandoffDestination): this {
this.destination = destination
return this
}

withShadowing(shadowing: boolean): this {
this.shadowing = shadowing
return this
}

build(): Handoff {
return new Handoff(
this.buildCommonFields(),
this.text,
this.onFinish,
this.destination,
this.shadowing
)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ContentType } from '../cms'
import { HandoffQueue } from '../contents'
import {
CarouselBuilder,
DocumentBuilder,
ElementBuilder,
HandoffBuilder,
ImageBuilder,
StartUpBuilder,
TextBuilder,
Expand Down Expand Up @@ -234,3 +236,19 @@ export class RndDocumentBuilder extends DocumentBuilder {
return this
}
}

export class RndHandoffBuilder extends HandoffBuilder {
readonly topComponentBuilder = new RndTopContentBuilder()

constructor(name: string = rndStr(), text: string = rndStr()) {
super(rndStr(), name, text)
}

withRandomFields(): this {
this.onFinish = new ContentCallbackBuilder().build()
this.destination = new HandoffQueue(rndStr())
this.shadowing = rndBool()
this.topComponentBuilder.withRandomFields(this)
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DateRangeContent,
Document,
Element,
Handoff,
Image,
MessageContent,
PagingOptions,
Expand Down Expand Up @@ -92,6 +93,10 @@ export class FilteredCMS implements CMS {
return this.filterContent(content, Image, context)
}

handoff(id: string, context?: Context): Promise<Handoff> {
return this.cms.handoff(id, context)
}

url(id: string, context?: Context): Promise<Url> {
return this.cms.url(id, context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ContentsDelivery } from './contents/contents'
import { DateRangeDelivery } from './contents/date-range'
import { DocumentDelivery } from './contents/document'
import { FollowUpDelivery } from './contents/follow-up'
import { HandoffDelivery } from './contents/handoff'
import { ImageDelivery } from './contents/image'
import { QueueDelivery } from './contents/queue'
import { ScheduleDelivery } from './contents/schedule'
Expand Down Expand Up @@ -50,6 +51,7 @@ export class Contentful implements cms.CMS {
private readonly _schedule: ScheduleDelivery
private readonly _dateRange: DateRangeDelivery
private readonly _image: ImageDelivery
private readonly _handoff: HandoffDelivery
private readonly _asset: AssetDelivery
private readonly _queue: QueueDelivery
private readonly _button: ButtonDelivery
Expand Down Expand Up @@ -81,6 +83,7 @@ export class Contentful implements cms.CMS {
this._startUp = new StartUpDelivery(delivery, this._button, resumeErrors)
this._url = new UrlDelivery(delivery, resumeErrors)
this._image = new ImageDelivery(delivery, resumeErrors)
this._handoff = new HandoffDelivery(delivery, resumeErrors)
this._asset = new AssetDelivery(delivery, resumeErrors)
this._schedule = new ScheduleDelivery(delivery, resumeErrors)
this._queue = new QueueDelivery(delivery, this._schedule, resumeErrors)
Expand All @@ -98,6 +101,7 @@ export class Contentful implements cms.CMS {
this._carousel,
this._image,
this._startUp,
this._handoff,
].forEach(d => d.setFollowUp(followUp))
this._keywords = new KeywordsDelivery(delivery)
this._dateRange = new DateRangeDelivery(delivery, resumeErrors)
Expand Down Expand Up @@ -146,6 +150,10 @@ export class Contentful implements cms.CMS {
return this._text.text(id, context)
}

async handoff(id: string, context = DEFAULT_CONTEXT): Promise<cms.Handoff> {
return this._handoff.handoff(id, context)
}

topContents<T extends TopContent>(
model: TopContentType,
context = DEFAULT_CONTEXT,
Expand Down Expand Up @@ -197,6 +205,8 @@ export class Contentful implements cms.CMS {
return retype(await this._text.fromEntry(entry, context))
case ContentType.IMAGE:
return retype(await this._image.fromEntry(entry, context))
case ContentType.HANDOFF:
return retype(this._handoff.fromEntry(entry, context))
case ContentType.URL:
return retype(await this._url.fromEntry(entry, context))
case ContentType.STARTUP:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
ContentWithNameFields,
} from '../delivery-utils'
import { DeliveryApi } from '../index'
import { getTargetCallback } from './callback-delivery'
import { CarouselFields } from './carousel'
import { HandoffFields } from './handoff'
import { QueueFields } from './queue'
import { HourRangeFields, ScheduleFields } from './schedule'
import { StartUpFields } from './startup'
Expand All @@ -20,7 +22,6 @@ import { UrlFields } from './url'

export class ButtonDelivery extends ContentDelivery {
public static BUTTON_CONTENT_TYPE = 'button'
private static PAYLOAD_CONTENT_TYPE = 'payload'

constructor(delivery: DeliveryApi, resumeErrors: boolean) {
super(cms.ContentType.BUTTON, delivery, resumeErrors)
Expand Down Expand Up @@ -82,7 +83,7 @@ export class ButtonDelivery extends ContentDelivery {
)
}
// target may be empty if we got it from a reference (delivery does not provide infinite recursive references)
const callback = this.getTargetCallback(buttonEntry.fields.target, context)
const callback = getTargetCallback(buttonEntry.fields.target, context)
return new cms.Button(
buttonEntry.sys.id,
buttonEntry.fields.name,
Expand Down Expand Up @@ -115,37 +116,6 @@ export class ButtonDelivery extends ContentDelivery {
}
return new cms.ContentCallback(modelType, entry.sys.id)
}

private getTargetCallback(
target: ButtonTarget,
context: cms.Context
): cms.Callback {
const model = ContentfulEntryUtils.getContentModel(target) as string
try {
switch (model) {
case ContentType.URL: {
const urlFields = target as contentful.Entry<UrlFields>
if (!urlFields.fields.url && context.ignoreFallbackLocale) {
return cms.Callback.empty()
}
return cms.Callback.ofUrl(urlFields.fields.url || '')
}
case ButtonDelivery.PAYLOAD_CONTENT_TYPE: {
const payloadFields = target as contentful.Entry<PayloadFields>
return cms.Callback.ofPayload(payloadFields.fields.payload)
}
}
if (isOfType(model, TopContentType)) {
return new cms.ContentCallback(model, target.sys.id)
}
throw new Error('Unexpected type: ' + model)
} catch (e) {
throw new CmsException(
`Error delivering button with id '${target.sys.id}'`,
e
)
}
}
}

export interface PayloadFields {
Expand All @@ -161,6 +131,7 @@ type ButtonTarget = contentful.Entry<
| QueueFields
| HourRangeFields
| ScheduleFields
| HandoffFields
>

export interface ButtonFields extends ContentWithNameFields {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as contentful from 'contentful'

import * as cms from '../../cms'
import { CmsException, ContentType } from '../../cms'
import { TopContentType } from '../../cms/cms'
import { isOfType } from '../../util/enums'
import { ContentfulEntryUtils } from '../delivery-utils'
import { PayloadFields } from './button'
import { CarouselFields } from './carousel'
import { HandoffFields } from './handoff'
import { QueueFields } from './queue'
import { HourRangeFields, ScheduleFields } from './schedule'
import { StartUpFields } from './startup'
import { TextFields } from './text'
import { UrlFields } from './url'

export type CallbackTarget = contentful.Entry<
| CarouselFields
| TextFields
| UrlFields
| PayloadFields
| StartUpFields
| QueueFields
| HourRangeFields
| ScheduleFields
| HandoffFields
>

export function getTargetCallback(
target: CallbackTarget,
context: cms.Context
): cms.Callback {
const PAYLOAD_CONTENT_TYPE = 'payload'
const model = ContentfulEntryUtils.getContentModel(target) as string
try {
switch (model) {
case ContentType.URL: {
const urlFields = target as contentful.Entry<UrlFields>
if (!urlFields.fields.url && context.ignoreFallbackLocale) {
return cms.Callback.empty()
}
return cms.Callback.ofUrl(urlFields.fields.url || '')
}
case PAYLOAD_CONTENT_TYPE: {
const payloadFields = target as contentful.Entry<PayloadFields>
return cms.Callback.ofPayload(payloadFields.fields.payload)
}
}
if (isOfType(model, TopContentType)) {
return new cms.ContentCallback(model, target.sys.id)
}
throw new Error('Unexpected type: ' + model)
} catch (e) {
throw new CmsException(
`Error delivering button with id '${target.sys.id}'`,
e
)
}
}
Loading

0 comments on commit 1fc4b00

Please sign in to comment.