implement basic call-center logic

This commit is contained in:
Nils Norman Haukås 2023-03-23 21:17:24 +01:00
parent ed71f7b2df
commit 5f099bc86d
9 changed files with 124 additions and 42 deletions

View file

@ -8,6 +8,8 @@ module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight)
eleventyConfig.addWatchTarget('./enterprise/')
eleventyConfig.addAsyncShortcode(
'include-code',
async function (relativePath, highlightLanguage = 'js') {

View file

@ -0,0 +1,43 @@
export class CallCenter {
queue = []
receptionists = []
callerCalling(caller) {
this.queue.push(caller)
}
receptionistSigningIn(receptionist) {
this.receptionists.push(receptionist)
}
receptionistSigningOff(receptionist) {
this.receptionists = this.receptionists.filter(
(item) => item !== receptionist
)
}
isQueued(caller) {
return !!this.queue.find((item) => item === caller)
}
handleCaller() {
const receptionist = this.receptionists.find((r) => r.talkingTo === null)
if (!receptionist) {
return false
}
const caller = this.queue.shift()
receptionist.talkTo(caller)
return true
}
lunchbreak() {
this.receptionists.forEach((r) => {
if (r.talkingTo === null) {
return
}
this.queue.push(r.talkingTo)
r.hangup()
})
}
}

View file

@ -0,0 +1,7 @@
export class Caller {
#srcNumber = null
constructor(srcNumber) {
this.#srcNumber = srcNumber
}
}

View file

@ -0,0 +1,3 @@
export * from './caller.mjs'
export * from './call-center.mjs'
export * from './receptionist.mjs'

View file

@ -0,0 +1,17 @@
import { randomUUID } from 'node:crypto'
export class Receptionist {
talkingTo = null
constructor(callSign = randomUUID()) {
this.callSign = callSign
}
talkTo(caller) {
this.talkingTo = caller
}
hangup() {
this.talkingTo = null
}
}

View file

@ -1,8 +0,0 @@
/**
* EnterpriseFaxMachineSender is a class
*/
export class EnterpriseFaxMachineSender {
constructor() {
console.log('Hello world')
}
}

View file

@ -0,0 +1,51 @@
import test from 'ava'
import { Caller, CallCenter, Receptionist } from '../call-center/index.mjs'
test('caller calls in and is put in a queue', (t) => {
const center = new CallCenter()
const caller = new Caller(12345678)
center.callerCalling(caller)
t.is(center.isQueued(caller), true, 'caller was not queued')
})
test('receptionists can come and leave work', (t) => {
const center = new CallCenter()
const receptionist1 = new Receptionist()
const receptionist2 = new Receptionist()
center.receptionistSigningIn(receptionist1)
center.receptionistSigningIn(receptionist2)
t.is(center.receptionists.length, 2)
center.receptionistSigningOff(receptionist1)
t.is(center.receptionists.length, 1)
})
test('callers are handled in turn by receptionist', (t) => {
const center = new CallCenter()
const caller1 = new Caller(12345678)
const caller2 = new Caller(32145677)
center.callerCalling(caller1)
center.callerCalling(caller2)
const receptionist = new Receptionist()
center.receptionistSigningIn(receptionist)
center.handleCaller()
t.true(
receptionist.talkingTo === caller1,
'receptionist was not talking to correct caller'
)
})
test('lunch break puts all callers back in the queue', (t) => {
const center = new CallCenter()
const caller1 = new Caller(12345678)
const caller2 = new Caller(32145677)
center.callerCalling(caller1)
center.callerCalling(caller2)
const receptionist = new Receptionist()
center.receptionistSigningIn(receptionist)
center.handleCaller()
t.false(center.isQueued(caller1), 'caller should not be queued')
center.lunchbreak()
t.true(center.isQueued(caller1), 'caller was not back in queue')
})
// Lunch break will put callers back to wo

View file

@ -1,10 +0,0 @@
import test from 'ava'
import { EnterpriseFaxMachineSender } from '../fax/enterprise-fax-machine-sender.mjs'
test('foo', (t) => {
t.pass()
})
test('not throw', (t) => {
t.notThrows(() => new EnterpriseFaxMachineSender())
})

View file

@ -11,10 +11,6 @@ You stare out the window for a while.
Hmm. I might as well look at the code.
<button type="button" onclick="toggleTheCode()">look at the code</button>
<div id="the-code">
This is getting stranger and stranger. This looks like a JavaScript library
written in an object-oriented programming style. And this is what they want me
to document?
@ -25,23 +21,4 @@ is.
Ok. They want me to document. How about I write some poetry? That should do it.
{% include-code "../../enterprise/fax/" "js" %}
</div>
<script>
const codeDiv = document.querySelector('#the-code')
let hidden = true
function toggleTheCode() {
if (hidden) {
hidden = false
codeDiv.classList.add('animate__animated', 'animate__fadeIn')
codeDiv.style = ''
window.location.search="open"
return
}
hidden = true
codeDiv.classList.remove('animate__fadeIn')
codeDiv.style = 'visibility: hidden;'
}
</script>
{% include-code "../../enterprise/call-center/" "js" %}