diff --git a/package.json b/package.json index eb0ec19..a220d06 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "dimescheduler", "description": "The Dime.Scheduler SDK", - "version": "0.1.0", + "version": "0.1.1", "main": "./dist/index.ts", "types": "./dist/index.d.ts", "module": "./dist/esm/index.js", diff --git a/samples/pingpong/.gitignore b/samples/pingpong/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/samples/pingpong/.gitignore @@ -0,0 +1,175 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/samples/pingpong/README.md b/samples/pingpong/README.md new file mode 100644 index 0000000..fc693e8 --- /dev/null +++ b/samples/pingpong/README.md @@ -0,0 +1,34 @@ +# Dime.Scheduler SDK for JavaScript sample: Ping Pong + +## Getting started + +Install the dependencies + +```cmd +bun install +``` + +Create an `.env` file with the Mapbox and Dime.Scheduler API keys: + +``` +DS_API_KEY= +``` + +There are two commands that you can run: `stage` and `play`. + +The `stage` command is create the playing field. It requires three sets of information: the start date (parameter 's'), the end date (parameter 'e'), and a list of resources to create appointments for: + +``` +bun stage -s '2022-04-03' -e '2022-04-09' -r 'ARNOUD' -r 'ASSIA' -r 'ELSEMIEK' -r 'HESSEL' +``` + +The `play` command accepts the same parameters: + +``` +bun play -s '2022-04-03' -e '2022-04-09' -r 'ARNOUD' -r 'ASSIA' -r 'ELSEMIEK' -r 'HESSEL' +``` + +## Application in action + +![](./assets/pingpong.gif) + diff --git a/samples/pingpong/assets/pingpong.gif b/samples/pingpong/assets/pingpong.gif new file mode 100644 index 0000000..d517d6f Binary files /dev/null and b/samples/pingpong/assets/pingpong.gif differ diff --git a/samples/pingpong/bun.lockb b/samples/pingpong/bun.lockb new file mode 100644 index 0000000..249a94a Binary files /dev/null and b/samples/pingpong/bun.lockb differ diff --git a/samples/pingpong/package.json b/samples/pingpong/package.json new file mode 100644 index 0000000..1bc0e59 --- /dev/null +++ b/samples/pingpong/package.json @@ -0,0 +1,19 @@ +{ + "name": "pingpong", + "module": "index.ts", + "type": "module", + "scripts": { + "stage": "bun run stage.ts", + "play": "bun run play.ts" + }, + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "commander": "^12.0.0", + "dimescheduler": "^0.1.0" + } +} \ No newline at end of file diff --git a/samples/pingpong/play.ts b/samples/pingpong/play.ts new file mode 100644 index 0000000..0114eb4 --- /dev/null +++ b/samples/pingpong/play.ts @@ -0,0 +1,84 @@ +import { Command } from 'commander'; +import DimeSchedulerClient, { Environment } from 'dimescheduler'; +import { Appointment } from 'dimescheduler/models'; +const program = new Command(); + +const collect = (val: any, memo: any[]) => { + memo.push(val); + return memo; +} + +program + .requiredOption('-s, --start ', 'Start date') + .requiredOption('-e, --end ', 'End date') + .requiredOption('-r, --resources ', 'Resources', collect, []); + +program.parse(process.argv); + +const options = program.opts(); + +const createBall = async (client: DimeSchedulerClient, startDate: string) => { + const endOfDay = new Date(startDate); + endOfDay.setDate(endOfDay.getDate() + 1); + + const appointment = new Appointment(); + appointment.resourceNo = options.resources[1]; + appointment.start = startDate; + appointment.end = endOfDay.toISOString(); + appointment.sourceApp = "PINGPONG"; + appointment.sourceType = "PINGPONG"; + appointment.jobNo = "PINGPONG"; + appointment.taskNo = "PINGPONG"; + appointment.category = "PINGPONG"; + appointment.appointmentGuid = "dbc77ca8-f7b8-4bc7-9877-d63d57a81210"; + + await client.import(appointment); +} + +const client = new DimeSchedulerClient(Bun.env.DS_API_KEY as string, Environment.Test); + +const play = async () => { + let start = new Date(options.start); + start.setDate(start.getDate() + 1); + + let end = new Date(options.end); + end.setDate(end.getDate() - 1); + + const dates = getDateRange(start, end); + for (const date of dates) { + await createBall(client, date.toISOString().split('T')[0]); + await Bun.sleep(500); + } +} + +const getDateRange = (startDate: Date, endDate: Date): Date[] => { + const dates = []; + let currentDate = new Date(startDate); + + while (currentDate <= endDate) { + dates.push(new Date(currentDate)); + currentDate.setDate(currentDate.getDate() + 1); + } + + return [...dates, ...dates.slice().reverse()]; +} + +let iterations = 0; +async function repeatAsyncFunctionSynchronously() { + let finished = false; + let result; + + while (!finished) { + try { + result = await play(); + iterations++; + if (iterations > 5) + finished = true; + } catch (error) { + console.error("Error occurred:", error); + break; + } + } +} + +repeatAsyncFunctionSynchronously(); \ No newline at end of file diff --git a/samples/pingpong/stage.ts b/samples/pingpong/stage.ts new file mode 100644 index 0000000..5b40761 --- /dev/null +++ b/samples/pingpong/stage.ts @@ -0,0 +1,68 @@ +import { Command } from 'commander'; +import DimeSchedulerClient, { Environment } from 'dimescheduler'; +import { Appointment, Category, Job, Task } from 'dimescheduler/models'; +const program = new Command(); + +const collect = (val: any, memo: any[]) => { + memo.push(val); + return memo; +} + +program + .requiredOption('-s, --start ', 'Start date') + .requiredOption('-e, --end ', 'End date') + .requiredOption('-r, --resources ', 'Resources', collect, []); + +program.parse(process.argv); + +const options = program.opts(); +const createBallColor = async (client: DimeSchedulerClient) => { + const ballColor = new Category(); + ballColor.name = "PINGPONG"; + ballColor.color = "#FF65D4"; + await client.import(ballColor); +} + +// Need to create a job and a task for appointments to arrive on the planning board +const createJobTask = async (client: DimeSchedulerClient) => { + const job = new Job(); + job.SourceApp = "PINGPONG"; + job.SourceType = "PINGPONG"; + job.JobNo = "PINGPONG"; + job.ShortDescription = "PING PONG SAMPLE"; + await client.import(job); + + const task = new Task(); + task.sourceApp = "PINGPONG"; + task.sourceType = "PINGPONG"; + task.jobNo = "PINGPONG"; + task.taskNo = "PINGPONG"; + task.shortDescription = "PING PONG SAMPLE"; + await client.import(task); +} + +const createPlayer = async (client: DimeSchedulerClient, date: Date, resourceNo: string) => { + const start = new Date(date); + const endOfDay = new Date(date); + endOfDay.setDate(endOfDay.getDate() + 1); + + const appointment = new Appointment(); + appointment.resourceNo = resourceNo; + appointment.start = start.toISOString(); + appointment.end = endOfDay.toISOString(); + appointment.sourceApp = "PINGPONG"; + appointment.sourceType = "PINGPONG"; + appointment.jobNo = "PINGPONG"; + appointment.taskNo = "PINGPONG"; + + await client.import(appointment); +} + +const client = new DimeSchedulerClient(Bun.env.DS_API_KEY as string, Environment.Test); +await createJobTask(client); +await createBallColor(client); + +for (const resource of options.resources) { + await createPlayer(client, options.start, resource); + await createPlayer(client, options.end, resource); +} \ No newline at end of file diff --git a/samples/pingpong/tsconfig.json b/samples/pingpong/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/samples/pingpong/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +} diff --git a/src/lib/endpoints/import.ts b/src/lib/endpoints/import.ts index 180de4d..040f17a 100644 --- a/src/lib/endpoints/import.ts +++ b/src/lib/endpoints/import.ts @@ -17,8 +17,6 @@ class ImportEndPoint extends Endpoint { const data = params.map(x => x.toImportRequest(append ? CrudType.Append : CrudType.Delete)); const body = JSON.stringify(data); - console.log(body); - const headers = { 'X-API-KEY': this.apiKey, 'Content-Type': 'application/json',