@banch0u/core-project-test-repository 2.3.15 → 2.3.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,152 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import _typeof from "@babel/runtime/helpers/typeof";
3
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
4
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
5
+ // store/slices/policies.js
6
+ import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
7
+ import Services from "./service";
8
+ import { setLoading } from "../global";
9
+ import { errorMessage } from "../../../utils/message";
10
+ var initialState = {
11
+ // map: { "archive.navs.create": true, "archive.navs.delete": false, ... }
12
+ policy: {},
13
+ // whether initial check was completed (useful to avoid running again)
14
+ checked: false,
15
+ // optional: errors
16
+ error: null
17
+ };
18
+ export var checkPolicies = createAsyncThunk("policies/checkPolicies",
19
+ /*#__PURE__*/
20
+ // policies: array of policy strings
21
+ function () {
22
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(policies, _ref) {
23
+ var dispatch, rejectWithValue, results, promises, settled, errMsg, _t2;
24
+ return _regeneratorRuntime.wrap(function (_context2) {
25
+ while (1) switch (_context2.prev = _context2.next) {
26
+ case 0:
27
+ dispatch = _ref.dispatch, rejectWithValue = _ref.rejectWithValue;
28
+ _context2.prev = 1;
29
+ dispatch(setLoading(true));
30
+
31
+ // If Services provides a bulk check endpoint you can use it here instead.
32
+ // Otherwise we call Services.checkPolicy for each policy.
33
+ results = {}; // run requests in parallel (faster) but you can change to sequential if
34
+ // backend requires rate limiting / ordering
35
+ promises = policies.map(/*#__PURE__*/function () {
36
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(policyName) {
37
+ var res, _t;
38
+ return _regeneratorRuntime.wrap(function (_context) {
39
+ while (1) switch (_context.prev = _context.next) {
40
+ case 0:
41
+ _context.prev = 0;
42
+ _context.next = 1;
43
+ return Services.checkPolicy(policyName);
44
+ case 1:
45
+ res = _context.sent;
46
+ if (!(res && _typeof(res) === "object")) {
47
+ _context.next = 5;
48
+ break;
49
+ }
50
+ if (!("allowed" in res)) {
51
+ _context.next = 2;
52
+ break;
53
+ }
54
+ return _context.abrupt("return", [policyName, !!res.allowed]);
55
+ case 2:
56
+ if (!("data" in res && _typeof(res.data) === "object")) {
57
+ _context.next = 3;
58
+ break;
59
+ }
60
+ if (!(typeof res.data === "boolean")) {
61
+ _context.next = 3;
62
+ break;
63
+ }
64
+ return _context.abrupt("return", [policyName, res.data]);
65
+ case 3:
66
+ if (!("status" in res)) {
67
+ _context.next = 4;
68
+ break;
69
+ }
70
+ return _context.abrupt("return", [policyName, res.status === 200 || !!res.status]);
71
+ case 4:
72
+ return _context.abrupt("return", [policyName, !!res]);
73
+ case 5:
74
+ return _context.abrupt("return", [policyName, !!res]);
75
+ case 6:
76
+ _context.prev = 6;
77
+ _t = _context["catch"](0);
78
+ // treat failures as false, but you may want to rethrow or store error
79
+ console.error("checkPolicy error for", policyName, _t);
80
+ return _context.abrupt("return", [policyName, false]);
81
+ case 7:
82
+ case "end":
83
+ return _context.stop();
84
+ }
85
+ }, _callee, null, [[0, 6]]);
86
+ }));
87
+ return function (_x3) {
88
+ return _ref3.apply(this, arguments);
89
+ };
90
+ }());
91
+ _context2.next = 2;
92
+ return Promise.all(promises);
93
+ case 2:
94
+ settled = _context2.sent;
95
+ settled.forEach(function (_ref4) {
96
+ var _ref5 = _slicedToArray(_ref4, 2),
97
+ name = _ref5[0],
98
+ allowed = _ref5[1];
99
+ results[name] = allowed;
100
+ });
101
+ dispatch(setLoading(false));
102
+ return _context2.abrupt("return", results);
103
+ case 3:
104
+ _context2.prev = 3;
105
+ _t2 = _context2["catch"](1);
106
+ dispatch(setLoading(false));
107
+ errMsg = _t2 && _t2.response && _t2.response.data && _t2.response.data.message || _t2.message || "Policy check failed";
108
+ errorMessage(errMsg);
109
+ return _context2.abrupt("return", rejectWithValue(errMsg));
110
+ case 4:
111
+ case "end":
112
+ return _context2.stop();
113
+ }
114
+ }, _callee2, null, [[1, 3]]);
115
+ }));
116
+ return function (_x, _x2) {
117
+ return _ref2.apply(this, arguments);
118
+ };
119
+ }());
120
+ var policy = createSlice({
121
+ name: "policy",
122
+ initialState: initialState,
123
+ reducers: {
124
+ // optional manual setter if needed
125
+ setPolicies: function setPolicies(state, action) {
126
+ state.policy = action.payload;
127
+ state.checked = true;
128
+ state.error = null;
129
+ },
130
+ resetPolicies: function resetPolicies(state) {
131
+ state.policy = {};
132
+ state.checked = false;
133
+ state.error = null;
134
+ }
135
+ },
136
+ extraReducers: function extraReducers(builder) {
137
+ builder.addCase(checkPolicies.fulfilled, function (state, action) {
138
+ state.policy = action.payload || {};
139
+ state.checked = true;
140
+ state.error = null;
141
+ }).addCase(checkPolicies.rejected, function (state, action) {
142
+ var _action$error;
143
+ state.error = action.payload || ((_action$error = action.error) === null || _action$error === void 0 ? void 0 : _action$error.message);
144
+ state.checked = true; // we attempted check but failed
145
+ });
146
+ }
147
+ });
148
+ var _policy$actions = policy.actions,
149
+ setPolicies = _policy$actions.setPolicies,
150
+ resetPolicies = _policy$actions.resetPolicies;
151
+ export { setPolicies, resetPolicies };
152
+ export default policy.reducer;
@@ -0,0 +1,56 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
5
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
6
+ import api from "../../../utils/axios";
7
+ var Services = /*#__PURE__*/_createClass(function Services() {
8
+ _classCallCheck(this, Services);
9
+ });
10
+ _defineProperty(Services, "checkPolicy", /*#__PURE__*/function () {
11
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(policy) {
12
+ var response, _error$response, _t;
13
+ return _regeneratorRuntime.wrap(function (_context) {
14
+ while (1) switch (_context.prev = _context.next) {
15
+ case 0:
16
+ _context.prev = 0;
17
+ _context.next = 1;
18
+ return api.post("/profile/policycheck", {
19
+ policy: policy
20
+ });
21
+ case 1:
22
+ response = _context.sent;
23
+ if (!((response === null || response === void 0 ? void 0 : response.status) === 204)) {
24
+ _context.next = 2;
25
+ break;
26
+ }
27
+ return _context.abrupt("return", true);
28
+ case 2:
29
+ if (!((response === null || response === void 0 ? void 0 : response.status) === 404)) {
30
+ _context.next = 3;
31
+ break;
32
+ }
33
+ return _context.abrupt("return", false);
34
+ case 3:
35
+ return _context.abrupt("return", false);
36
+ case 4:
37
+ _context.prev = 4;
38
+ _t = _context["catch"](0);
39
+ if (!(((_error$response = _t.response) === null || _error$response === void 0 ? void 0 : _error$response.status) === 404)) {
40
+ _context.next = 5;
41
+ break;
42
+ }
43
+ return _context.abrupt("return", false);
44
+ case 5:
45
+ throw _t;
46
+ case 6:
47
+ case "end":
48
+ return _context.stop();
49
+ }
50
+ }, _callee, null, [[0, 4]]);
51
+ }));
52
+ return function (_x) {
53
+ return _ref.apply(this, arguments);
54
+ };
55
+ }());
56
+ export default Services;
@@ -6,7 +6,8 @@ import { notification } from "./slices/notification";
6
6
  import { companyInfo } from "./slices/companyInfo";
7
7
  import { questionnaire } from "./slices/questionnaire";
8
8
  import { employees } from "./slices/employees";
9
+ import policy from "./slices/policy";
9
10
  export var store = configureStore({
10
- reducer: _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, employees.name, employees.reducer), notification.name, notification.reducer), global.name, global.reducer), auth.name, auth.reducer), companyInfo.name, companyInfo.reducer), questionnaire.name, questionnaire.reducer)
11
+ reducer: _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, employees.name, employees.reducer), notification.name, notification.reducer), global.name, global.reducer), auth.name, auth.reducer), companyInfo.name, companyInfo.reducer), questionnaire.name, questionnaire.reducer), "policy", policy)
11
12
  });
12
13
  export default store;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@banch0u/core-project-test-repository",
3
- "version": "2.3.15",
3
+ "version": "2.3.16",
4
4
  "description": "Shared core features for all projects",
5
5
  "main": "dist/index.js",
6
6
  "files": [