From 1213b58c37fcec8f5f6298e6e66103150a9db61e Mon Sep 17 00:00:00 2001 From: Joe Spanbauer Date: Tue, 22 Oct 2024 16:09:01 -0400 Subject: [PATCH 1/5] feat(user-activity-broadcaster): adding package EB-719 --- packages/user-activity-broadcaster/README.md | 25 ++++++ .../user-activity-broadcaster/jest.config.js | 7 ++ .../user-activity-broadcaster/package.json | 26 +++++++ .../user-activity-broadcaster/project.json | 39 ++++++++++ .../user-activity-broadcaster/src/index.d.ts | 1 + .../user-activity-broadcaster/src/index.js | 51 +++++++++++++ .../src/tests/index.test.js | 76 +++++++++++++++++++ .../user-activity-broadcaster/tsconfig.json | 5 ++ .../tsconfig.spec.json | 10 +++ yarn.lock | 8 ++ 10 files changed, 248 insertions(+) create mode 100644 packages/user-activity-broadcaster/README.md create mode 100644 packages/user-activity-broadcaster/jest.config.js create mode 100644 packages/user-activity-broadcaster/package.json create mode 100644 packages/user-activity-broadcaster/project.json create mode 100644 packages/user-activity-broadcaster/src/index.d.ts create mode 100644 packages/user-activity-broadcaster/src/index.js create mode 100644 packages/user-activity-broadcaster/src/tests/index.test.js create mode 100644 packages/user-activity-broadcaster/tsconfig.json create mode 100644 packages/user-activity-broadcaster/tsconfig.spec.json diff --git a/packages/user-activity-broadcaster/README.md b/packages/user-activity-broadcaster/README.md new file mode 100644 index 000000000..9fbb65c7e --- /dev/null +++ b/packages/user-activity-broadcaster/README.md @@ -0,0 +1,25 @@ +# @availity/user-activity-broadcaster + +> This package broadcasts user activity to the navigation. + +[![Version](https://img.shields.io/npm/v/@availity/user-activity-broadcaster.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/user-activity-broadcaster) +[![NPM Downloads](https://img.shields.io/npm/dt/@availity/user-activity-broadcaster.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/user-activity-broadcaster) +[![Dependecy Status](https://img.shields.io/librariesio/release/npm/@availity/user-activity-broadcaster?style=for-the-badge)](https://github.com/Availity/sdk-js/blob/master/packages/user-activity-broadcaster/package.json) + +## Installation + +### NPM + +```bash +npm install @availity/user-activity-broadcaster +``` + +### Yarn + +```bash +yarn add @availity/user-activity-broadcaster +``` + +## Usage + +> All you have to do is include this as a dependency and import it into the root of your web application. diff --git a/packages/user-activity-broadcaster/jest.config.js b/packages/user-activity-broadcaster/jest.config.js new file mode 100644 index 000000000..98dbc5969 --- /dev/null +++ b/packages/user-activity-broadcaster/jest.config.js @@ -0,0 +1,7 @@ +const global = require('../../jest.config'); + +module.exports = { + ...global, + displayName: 'user-activity-broadcaster', + coverageDirectory: '../../coverage/user-activity-broadcaster', +}; diff --git a/packages/user-activity-broadcaster/package.json b/packages/user-activity-broadcaster/package.json new file mode 100644 index 000000000..223e9a36f --- /dev/null +++ b/packages/user-activity-broadcaster/package.json @@ -0,0 +1,26 @@ +{ + "name": "@availity/user-activity-broadcaster", + "version": "1.0.0-alpha.0", + "description": "This package broacasts user activity to the navigation.", + "main": "src/user-activity-broadcaster.js", + "keywords": [], + "author": "Joe Spanbauer ", + "engines": { + "node": "^18.0.0 || ^20.0.0" + }, + "scripts": { + "build": "tsup src/index.js --format esm,cjs --dts", + "dev": "tsup src/index.js --format esm,cjs --watch --dts", + "lint": "eslint src", + "lint:fix": "eslint src --fix", + "clean": "rm -rf node_modules && rm -rf dist", + "publish": "yarn npm publish --tolerate-republish --access public" + }, + "license": "MIT", + "devDependencies": { + "tsup": "^7.2.0" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/user-activity-broadcaster/project.json b/packages/user-activity-broadcaster/project.json new file mode 100644 index 000000000..dd935e552 --- /dev/null +++ b/packages/user-activity-broadcaster/project.json @@ -0,0 +1,39 @@ +{ + "name": "@availity/user-activity-broadcaster", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "projectType": "library", + "targets": { + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/resolve-url"], + "options": { + "jestConfig": "packages/user-activity-broadcaster/jest.config.js" + } + }, + "version": { + "executor": "@jscutlery/semver:version", + "options": { + "preset": "angular", + "commitMessageFormat": "chore({projectName}): release version ${version} [skip ci]", + "tagPrefix": "{projectName}@", + "baseBranch": "master", + "trackDeps": true + } + }, + "lint": { + "executor": "@nx/eslint:lint", + "options": { + "eslintConfig": ".eslintrc.yaml", + "silent": false, + "fix": false, + "cache": true, + "cacheLocation": "./node_modules/.cache/user-activity-broadcaster/.eslintcache", + "maxWarnings": -1, + "quiet": false, + "noEslintrc": false, + "hasTypeAwareRules": true, + "cacheStrategy": "metadata" + } + } + } +} diff --git a/packages/user-activity-broadcaster/src/index.d.ts b/packages/user-activity-broadcaster/src/index.d.ts new file mode 100644 index 000000000..53594fa0f --- /dev/null +++ b/packages/user-activity-broadcaster/src/index.d.ts @@ -0,0 +1 @@ +/* eslint-disable unicorn/prefer-export-from */ diff --git a/packages/user-activity-broadcaster/src/index.js b/packages/user-activity-broadcaster/src/index.js new file mode 100644 index 000000000..5d16eb3d7 --- /dev/null +++ b/packages/user-activity-broadcaster/src/index.js @@ -0,0 +1,51 @@ +const MINUTE = 60 * 1000 +const interval = MINUTE * 5 +export const eventName = 'user_activity' + +// Must be a dynamic object to test this +export const lastActivity = {} + +export const getTargetOrigin = (origin = window.location.origin) => { + // Setup targetOrigin to alternate origin (because the same origin already works) + if (origin) { + if (origin.includes('apps')) { + return origin.replace('apps', 'essentials') + } + + if (origin.includes('essentials')) { + return origin.replace('essentials', 'apps') + } + } + + return undefined +} + +const targetOrigin = getTargetOrigin() + +// PostMessage Logic +export const handleActivityUpdate = () => { + window.top.postMessage({ + event: eventName, + time: lastActivity.time + }, targetOrigin) +} + +// Debounce Logic +let activityIntervalId = setInterval(handleActivityUpdate, interval) +// Re-assignable for testing +export const updateInterval = (newInterval) => { + clearInterval(activityIntervalId) + activityIntervalId = setInterval(handleActivityUpdate, newInterval) +} + +// Event Handlers +export const handleActivity = () => { + lastActivity.time = Date.now().toString() +} + +// Add ability to test handleActivity and events +export const addEventListeners = () => { + document.addEventListener('mousedown', handleActivity) + document.addEventListener('keydown', handleActivity) +} +addEventListeners() diff --git a/packages/user-activity-broadcaster/src/tests/index.test.js b/packages/user-activity-broadcaster/src/tests/index.test.js new file mode 100644 index 000000000..8ea3d15eb --- /dev/null +++ b/packages/user-activity-broadcaster/src/tests/index.test.js @@ -0,0 +1,76 @@ +const userActivityBroadcaster = require('../index') + +const { + getTargetOrigin, + handleActivity, + updateInterval, + addEventListeners, + lastActivity +} = userActivityBroadcaster + +describe('user-activity-broadcaster', () => { + describe('targetOrigin', () => { + test('essentials.availity.com origin should have targetOrigion of apps', () => { + const testOrigin = 'essentials.availity.com' + const expected = 'apps.availity.com' + + const targetOrigin = getTargetOrigin(testOrigin) + + expect(targetOrigin).toBe(expected) + }) + + test('apps.availity.com origin should have targetOrigion of essentials', () => { + const testOrigin = 'apps.availity.com' + const expected = 'essentials.availity.com' + + const targetOrigin = getTargetOrigin(testOrigin) + + expect(targetOrigin).toBe(expected) + }) + }) + + test('should call handleActivityUpdate every interval', async () => { + const testInterval = 1000 + const waitTime = 2999 + const expectedCallCount = 2 + + window.top.postMessage = () => {} + + const spy = jest.spyOn(userActivityBroadcaster, 'handleActivityUpdate') + + updateInterval(testInterval) + + await new Promise((resolve) => { + setTimeout(() => { + resolve() + }, waitTime) + }) + + expect(spy).toHaveBeenCalledTimes(expectedCallCount) + }) + + test('keydown events call handleActivity', async () => { + const spy = jest.spyOn(userActivityBroadcaster, 'handleActivity') + + addEventListeners() + + document.dispatchEvent(new MouseEvent('keydown')) + + expect(spy).toHaveBeenCalled() + }) + + test('mousedown events call handleActivity', async () => { + const spy = jest.spyOn(userActivityBroadcaster, 'handleActivity') + + addEventListeners() + + document.dispatchEvent(new MouseEvent('mousedown')) + + expect(spy).toHaveBeenCalled() + }) + + test('handleActivity should update handleActivity', () => { + handleActivity() + expect(lastActivity.time).toBeDefined() + }) +}) diff --git a/packages/user-activity-broadcaster/tsconfig.json b/packages/user-activity-broadcaster/tsconfig.json new file mode 100644 index 000000000..b70853b01 --- /dev/null +++ b/packages/user-activity-broadcaster/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../../tsconfig.json", + "include": ["."], + "exclude": ["dist", "build", "node_modules"] +} diff --git a/packages/user-activity-broadcaster/tsconfig.spec.json b/packages/user-activity-broadcaster/tsconfig.spec.json new file mode 100644 index 000000000..a85a3f595 --- /dev/null +++ b/packages/user-activity-broadcaster/tsconfig.spec.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"], + "allowJs": true + }, + "include": ["**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] +} diff --git a/yarn.lock b/yarn.lock index 0223710e9..8f291e031 100644 --- a/yarn.lock +++ b/yarn.lock @@ -446,6 +446,14 @@ __metadata: languageName: unknown linkType: soft +"@availity/user-activity-broadcaster@workspace:packages/user-activity-broadcaster": + version: 0.0.0-use.local + resolution: "@availity/user-activity-broadcaster@workspace:packages/user-activity-broadcaster" + dependencies: + tsup: ^7.2.0 + languageName: unknown + linkType: soft + "@availity/yup@workspace:packages/yup": version: 0.0.0-use.local resolution: "@availity/yup@workspace:packages/yup" From 16070049522d8db8401d71f7d5d074dc0bc213c4 Mon Sep 17 00:00:00 2001 From: Joe Spanbauer Date: Tue, 22 Oct 2024 16:10:55 -0400 Subject: [PATCH 2/5] feat(user-activity-broadcaster): adding changelog file EB-719 --- packages/user-activity-broadcaster/CHANGELOG.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 packages/user-activity-broadcaster/CHANGELOG.md diff --git a/packages/user-activity-broadcaster/CHANGELOG.md b/packages/user-activity-broadcaster/CHANGELOG.md new file mode 100644 index 000000000..e69de29bb From b66e9b87c2dcd305cd1ccbba5980e7550cc1bd70 Mon Sep 17 00:00:00 2001 From: Joe Spanbauer Date: Tue, 22 Oct 2024 16:46:48 -0400 Subject: [PATCH 3/5] feat(user-activity-broadcaster): adding ignore because we're not using typescript EB-719 --- packages/user-activity-broadcaster/src/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/user-activity-broadcaster/src/index.d.ts b/packages/user-activity-broadcaster/src/index.d.ts index 53594fa0f..4f36ee138 100644 --- a/packages/user-activity-broadcaster/src/index.d.ts +++ b/packages/user-activity-broadcaster/src/index.d.ts @@ -1 +1 @@ -/* eslint-disable unicorn/prefer-export-from */ +/* eslint-disable unicorn/no-empty-file */ From 6e9519d39a88d38cdf3cc052872225820961628d Mon Sep 17 00:00:00 2001 From: Joe Spanbauer Date: Tue, 22 Oct 2024 16:48:20 -0400 Subject: [PATCH 4/5] chore(user-activity-broadcaster): release version 0.1.0-alpha.0 [skip ci] --- .../user-activity-broadcaster/CHANGELOG.md | 23 +++++++++++++++++++ .../user-activity-broadcaster/package.json | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/user-activity-broadcaster/CHANGELOG.md b/packages/user-activity-broadcaster/CHANGELOG.md index e69de29bb..4a053c754 100644 --- a/packages/user-activity-broadcaster/CHANGELOG.md +++ b/packages/user-activity-broadcaster/CHANGELOG.md @@ -0,0 +1,23 @@ +# Changelog + +This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). + +# 0.1.0-alpha.0 (2024-10-22) + + +### Features + +* **user-activity-broadcaster:** adding changelog file EB-719 ([1607004](https://github.com/Availity/sdk-js/commit/16070049522d8db8401d71f7d5d074dc0bc213c4)) +* **user-activity-broadcaster:** adding ignore because we're not using typescript EB-719 ([b66e9b8](https://github.com/Availity/sdk-js/commit/b66e9b87c2dcd305cd1ccbba5980e7550cc1bd70)) +* **user-activity-broadcaster:** adding package EB-719 ([1213b58](https://github.com/Availity/sdk-js/commit/1213b58c37fcec8f5f6298e6e66103150a9db61e)) + + + +# 0.1.0-alpha.0 (2024-10-22) + + +### Features + +* **user-activity-broadcaster:** adding changelog file EB-719 ([1607004](https://github.com/Availity/sdk-js/commit/16070049522d8db8401d71f7d5d074dc0bc213c4)) +* **user-activity-broadcaster:** adding ignore because we're not using typescript EB-719 ([b66e9b8](https://github.com/Availity/sdk-js/commit/b66e9b87c2dcd305cd1ccbba5980e7550cc1bd70)) +* **user-activity-broadcaster:** adding package EB-719 ([1213b58](https://github.com/Availity/sdk-js/commit/1213b58c37fcec8f5f6298e6e66103150a9db61e)) diff --git a/packages/user-activity-broadcaster/package.json b/packages/user-activity-broadcaster/package.json index 223e9a36f..08d12b94b 100644 --- a/packages/user-activity-broadcaster/package.json +++ b/packages/user-activity-broadcaster/package.json @@ -1,6 +1,6 @@ { "name": "@availity/user-activity-broadcaster", - "version": "1.0.0-alpha.0", + "version": "0.1.0-alpha.0", "description": "This package broacasts user activity to the navigation.", "main": "src/user-activity-broadcaster.js", "keywords": [], From be9d03ebcd1efd352eed7c1da46a3addf53b8fc8 Mon Sep 17 00:00:00 2001 From: Joe Spanbauer Date: Tue, 22 Oct 2024 16:48:58 -0400 Subject: [PATCH 5/5] feat(user-activity-broadcaster): fixing commit message EB-719 --- packages/user-activity-broadcaster/project.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/user-activity-broadcaster/project.json b/packages/user-activity-broadcaster/project.json index dd935e552..1d616949d 100644 --- a/packages/user-activity-broadcaster/project.json +++ b/packages/user-activity-broadcaster/project.json @@ -14,7 +14,7 @@ "executor": "@jscutlery/semver:version", "options": { "preset": "angular", - "commitMessageFormat": "chore({projectName}): release version ${version} [skip ci]", + "commitMessageFormat": "chore(user-activity-broadcaster): release version ${version} [skip ci]", "tagPrefix": "{projectName}@", "baseBranch": "master", "trackDeps": true