cloudcommerce 0.0.74 → 0.0.76
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.
- package/.vscode/extensions.json +2 -1
- package/.vscode/settings.json +9 -0
- package/CHANGELOG.md +19 -0
- package/package.json +1 -1
- package/packages/api/package.json +1 -1
- package/packages/apps/correios/package.json +1 -1
- package/packages/apps/custom-shipping/package.json +1 -1
- package/packages/apps/discounts/package.json +1 -1
- package/packages/apps/frenet/package.json +1 -1
- package/packages/apps/tiny-erp/lib/event-to-tiny.js +115 -0
- package/packages/apps/tiny-erp/lib/event-to-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/index.js +2 -0
- package/packages/apps/tiny-erp/lib/index.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/after-tiny-queue.js +79 -0
- package/packages/apps/tiny-erp/lib/integration/after-tiny-queue.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/export-order-to-tiny.js +84 -0
- package/packages/apps/tiny-erp/lib/integration/export-order-to-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/export-product-to-tiny.js +58 -0
- package/packages/apps/tiny-erp/lib/integration/export-product-to-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/helpers/format-tiny-date.js +7 -0
- package/packages/apps/tiny-erp/lib/integration/helpers/format-tiny-date.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/import-order-from-tiny.js +92 -0
- package/packages/apps/tiny-erp/lib/integration/import-order-from-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/import-product-from-tiny.js +158 -0
- package/packages/apps/tiny-erp/lib/integration/import-product-from-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/order-from-tiny.js +46 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/order-from-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/order-to-tiny.js +193 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/order-to-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/product-from-tiny.js +199 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/product-from-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/product-to-tiny.js +129 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/product-to-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/status-from-tiny.js +34 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/status-from-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/status-to-tiny.js +39 -0
- package/packages/apps/tiny-erp/lib/integration/parsers/status-to-tiny.js.map +1 -0
- package/packages/apps/tiny-erp/lib/integration/post-tiny-erp.js +47 -0
- package/packages/apps/tiny-erp/lib/integration/post-tiny-erp.js.map +1 -0
- package/packages/apps/tiny-erp/lib/tiny-erp.js +18 -0
- package/packages/apps/tiny-erp/lib/tiny-erp.js.map +1 -0
- package/packages/apps/tiny-erp/lib/tiny-webhook.js +92 -0
- package/packages/apps/tiny-erp/lib/tiny-webhook.js.map +1 -0
- package/packages/apps/tiny-erp/package.json +1 -1
- package/packages/cli/config/firebase.json +7 -0
- package/packages/cli/package.json +1 -1
- package/packages/config/package.json +1 -1
- package/packages/events/package.json +1 -1
- package/packages/firebase/package.json +1 -1
- package/packages/modules/lib/firebase/ajv.js +34 -0
- package/packages/modules/lib/firebase/ajv.js.map +1 -0
- package/packages/modules/lib/firebase/call-app-module.js +135 -0
- package/packages/modules/lib/firebase/call-app-module.js.map +1 -0
- package/packages/modules/lib/firebase/checkout.js +1 -0
- package/packages/modules/lib/firebase/checkout.js.map +1 -0
- package/packages/modules/lib/firebase/handle-module.js +169 -0
- package/packages/modules/lib/firebase/handle-module.js.map +1 -0
- package/packages/modules/lib/firebase/proxy-apps.js +1 -0
- package/packages/modules/lib/firebase/proxy-apps.js.map +1 -0
- package/packages/modules/lib/firebase/serve-modules-api.js +58 -0
- package/packages/modules/lib/firebase/serve-modules-api.js.map +1 -0
- package/packages/modules/lib/firebase.js +13 -0
- package/packages/modules/lib/firebase.js.map +1 -0
- package/packages/modules/lib/index.js +25 -0
- package/packages/modules/lib/index.js.map +1 -0
- package/packages/modules/package.json +1 -1
- package/packages/passport/lib/firebase/handle-passport.js +92 -96
- package/packages/passport/lib/firebase/serve-passport-api.js +34 -35
- package/packages/passport/lib/firebase.js +7 -5
- package/packages/passport/lib/index.js +1 -2
- package/packages/passport/package.json +1 -1
- package/packages/ssr/lib/firebase/serve-storefront.js +33 -33
- package/packages/ssr/lib/firebase.js +4 -2
- package/packages/ssr/lib/index.js +2 -1
- package/packages/ssr/package.json +1 -1
- package/packages/storefront/astro.config.mjs +9 -3
- package/packages/storefront/dist/client/assets/_...3d331176.css +1 -0
- package/packages/storefront/dist/client/assets/{fallback.9cd2ed3f.css → _...b330a555.css} +1 -1
- package/packages/storefront/dist/client/assets/_...c1ec409f.css +4 -0
- package/packages/storefront/dist/client/assets/fallback-index.552fd862.css +1 -0
- package/packages/storefront/dist/client/assets/{_...27b7eb4d.css → fallback.a3f85892.css} +1 -1
- package/packages/storefront/dist/client/assets/{index.c5a9ea80.css → index.73f26ab5.css} +1 -1
- package/packages/storefront/dist/client/client.80baece3.js +1 -0
- package/packages/storefront/dist/client/hoisted.61a32b69.js +661 -0
- package/packages/storefront/dist/server/entry.mjs +326 -161
- package/packages/storefront/package.json +8 -3
- package/packages/storefront/src/html.d.ts +23 -0
- package/packages/storefront/src/lib/components/Card.astro +1 -1
- package/packages/storefront/src/lib/components/LoginModal.vue +14 -20
- package/packages/storefront/src/lib/layouts/Base.astro +129 -0
- package/packages/storefront/src/lib/layouts/Checkout.astro +0 -0
- package/packages/storefront/src/lib/layouts/Pages.astro +30 -0
- package/packages/storefront/src/lib/layouts/meta/Head.astro +62 -1
- package/packages/storefront/src/lib/layouts/meta/Json.astro +49 -10
- package/packages/storefront/src/lib/main/Home.astro +84 -0
- package/packages/storefront/src/lib/main/Wildcard.astro +0 -0
- package/packages/storefront/src/lib/ssr-context.ts +2 -1
- package/packages/storefront/src/lib/views/[...slug].astro +3 -3
- package/packages/storefront/src/lib/views/fallback.astro +5 -3
- package/packages/storefront/src/lib/views/index.astro +4 -3
- package/packages/storefront/src/pages/index.astro +7 -2
- package/packages/storefront/tailwind.config.ts +0 -0
- package/packages/storefront/uno.config.ts +98 -0
- package/packages/types/package.json +1 -1
- package/packages/storefront/dist/client/LoginModal.d6b935cf.js +0 -1
- package/packages/storefront/dist/client/assets/_...b3f65c5c.css +0 -1
- package/packages/storefront/dist/client/assets/fallback-index.90217cf0.css +0 -1
- package/packages/storefront/dist/client/chunks/index.esm.69f70489.js +0 -1726
- package/packages/storefront/dist/client/chunks/index.esm2017.1194d1a1.js +0 -457
- package/packages/storefront/dist/client/chunks/runtime-core.esm-bundler.a7337834.js +0 -1
- package/packages/storefront/dist/client/client.80b904f3.js +0 -1
- package/packages/storefront/dist/client/hoisted.24787260.js +0 -376
- package/packages/storefront/src/lib/layouts/Layout.astro +0 -84
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { logger } from 'firebase-functions';
|
|
2
|
+
import Ajv from 'ajv';
|
|
3
|
+
import addFormats from 'ajv-formats';
|
|
4
|
+
import api from '@cloudcommerce/api';
|
|
5
|
+
import config from '@cloudcommerce/firebase/lib/config';
|
|
6
|
+
import {
|
|
7
|
+
ajv, ajvOptions, parseAjvErrors, sendRequestError,
|
|
8
|
+
} from './ajv.js';
|
|
9
|
+
import callAppModule from './call-app-module.js';
|
|
10
|
+
|
|
11
|
+
const ajvAppsResponse = addFormats(new Ajv({ ...ajvOptions, allErrors: true }));
|
|
12
|
+
// Cache apps list and no params modules results
|
|
13
|
+
const appsCache = {};
|
|
14
|
+
const resultsCache = {};
|
|
15
|
+
async function runModule(params, res, modName, validate, responseValidate, appId) {
|
|
16
|
+
const respond = (result) => res.send({
|
|
17
|
+
result,
|
|
18
|
+
meta: params,
|
|
19
|
+
});
|
|
20
|
+
const { storeId } = config.get();
|
|
21
|
+
const isEmptyParams = (!params || !Object.keys(params).length);
|
|
22
|
+
if (!validate(params)) {
|
|
23
|
+
return sendRequestError(res, modName, validate.errors);
|
|
24
|
+
}
|
|
25
|
+
let canCache = true;
|
|
26
|
+
const cacheKey = `${storeId}:${modName}`;
|
|
27
|
+
const listAppsParams = {
|
|
28
|
+
state: 'active',
|
|
29
|
+
[`modules.${modName}.enabled`]: true,
|
|
30
|
+
fields: `_id,app_id,version,data,hidden_data,modules.${modName}`,
|
|
31
|
+
};
|
|
32
|
+
if (appId
|
|
33
|
+
&& (typeof appId === 'number' || (typeof appId === 'string' && /^\d+$/.test(appId)))) {
|
|
34
|
+
if (typeof appId === 'string') {
|
|
35
|
+
appId = parseInt(appId, 10);
|
|
36
|
+
}
|
|
37
|
+
canCache = false;
|
|
38
|
+
listAppsParams.app_id = appId;
|
|
39
|
+
listAppsParams.limit = 1;
|
|
40
|
+
}
|
|
41
|
+
let canCacheResults = false;
|
|
42
|
+
if (canCache && isEmptyParams) {
|
|
43
|
+
if (resultsCache[cacheKey]) {
|
|
44
|
+
return respond(resultsCache[cacheKey]);
|
|
45
|
+
}
|
|
46
|
+
canCacheResults = true;
|
|
47
|
+
}
|
|
48
|
+
let appsList;
|
|
49
|
+
if (canCache && appsCache[cacheKey]) {
|
|
50
|
+
appsList = appsCache[cacheKey];
|
|
51
|
+
} else {
|
|
52
|
+
try {
|
|
53
|
+
const { data } = await api.get('applications', {
|
|
54
|
+
params: listAppsParams,
|
|
55
|
+
});
|
|
56
|
+
appsList = data.result;
|
|
57
|
+
} catch (err) {
|
|
58
|
+
logger.error(err);
|
|
59
|
+
const error = err;
|
|
60
|
+
return res.status(500).send({
|
|
61
|
+
status: 500,
|
|
62
|
+
error_code: 'MOD801',
|
|
63
|
+
message: `Store API returned status ${error.statusCode} trying to list apps`,
|
|
64
|
+
more_info: error.data?.user_message?.en_us,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(appsList)) {
|
|
69
|
+
if (!appsList.length) {
|
|
70
|
+
return respond([]);
|
|
71
|
+
}
|
|
72
|
+
if (canCache && !appsCache[cacheKey]) {
|
|
73
|
+
appsCache[cacheKey] = appsList;
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
appsCache[cacheKey] = null;
|
|
76
|
+
delete appsCache[cacheKey];
|
|
77
|
+
}, appsList.length ? 60000 : 3000);
|
|
78
|
+
}
|
|
79
|
+
const moduleReqs = [];
|
|
80
|
+
for (let i = 0; i < appsList.length; i++) {
|
|
81
|
+
const application = appsList[i];
|
|
82
|
+
if (!application.hidden_data) {
|
|
83
|
+
application.hidden_data = {};
|
|
84
|
+
}
|
|
85
|
+
if (!application.data) {
|
|
86
|
+
application.data = {};
|
|
87
|
+
}
|
|
88
|
+
const appModuleUrl = application.modules[modName].endpoint;
|
|
89
|
+
// Handle request with big timeout if proxying one app (by ID) only
|
|
90
|
+
const isBigTimeout = !!(appId);
|
|
91
|
+
const appModuleBody = {
|
|
92
|
+
storeId,
|
|
93
|
+
module: modName,
|
|
94
|
+
params,
|
|
95
|
+
application,
|
|
96
|
+
};
|
|
97
|
+
const reqStartTime = Date.now();
|
|
98
|
+
moduleReqs.push(new Promise((resolve) => {
|
|
99
|
+
let response;
|
|
100
|
+
let isError = false;
|
|
101
|
+
let errorMessage = null;
|
|
102
|
+
callAppModule(appId, modName, appModuleUrl, appModuleBody, isBigTimeout)
|
|
103
|
+
.then((appResponse) => {
|
|
104
|
+
response = appResponse;
|
|
105
|
+
if (appResponse?.error) {
|
|
106
|
+
isError = true;
|
|
107
|
+
errorMessage = appResponse.message || String(appResponse.error);
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
.catch((err) => {
|
|
111
|
+
response = null;
|
|
112
|
+
isError = true;
|
|
113
|
+
errorMessage = err.message;
|
|
114
|
+
})
|
|
115
|
+
.finally(() => {
|
|
116
|
+
const result = {
|
|
117
|
+
_id: application._id,
|
|
118
|
+
app_id: application.app_id,
|
|
119
|
+
took: Date.now() - reqStartTime,
|
|
120
|
+
version: application.version,
|
|
121
|
+
validated: false,
|
|
122
|
+
response_errors: null,
|
|
123
|
+
error: isError,
|
|
124
|
+
error_message: errorMessage,
|
|
125
|
+
response,
|
|
126
|
+
};
|
|
127
|
+
if (!isError && typeof response === 'object' && response !== null) {
|
|
128
|
+
result.validated = responseValidate(response);
|
|
129
|
+
if (!result.validated) {
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
result.response_errors = parseAjvErrors(responseValidate.errors, ajvAppsResponse);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
resolve(result);
|
|
135
|
+
});
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
return Promise.all(moduleReqs).then((results) => {
|
|
139
|
+
if (!results.find(({ response }) => response)) {
|
|
140
|
+
res.status(409);
|
|
141
|
+
canCacheResults = false;
|
|
142
|
+
}
|
|
143
|
+
if (canCacheResults && !resultsCache[cacheKey]) {
|
|
144
|
+
resultsCache[cacheKey] = results;
|
|
145
|
+
setTimeout(() => {
|
|
146
|
+
resultsCache[cacheKey] = null;
|
|
147
|
+
delete resultsCache[cacheKey];
|
|
148
|
+
}, 60000);
|
|
149
|
+
}
|
|
150
|
+
return respond(results);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
// Shoud never happen
|
|
154
|
+
return res.sendStatus(501);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export default (modName, schema, responseSchema, req, res) => {
|
|
158
|
+
const validate = ajv.compile(schema);
|
|
159
|
+
const responseValidate = ajvAppsResponse.compile(responseSchema);
|
|
160
|
+
return {
|
|
161
|
+
GET() {
|
|
162
|
+
runModule(req.query, res, modName, validate, responseValidate);
|
|
163
|
+
},
|
|
164
|
+
POST() {
|
|
165
|
+
runModule(req.body, res, modName, validate, responseValidate, req.query.app_id);
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
// # sourceMappingURL=handle-module.js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Q,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,gBAAgB,GAAG,eAAe,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IACjE,OAAO;QACL,GAAG;YACD,SAAS,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,gBAAgB,CAAC,CAAC;QACjE,CAAC;QACD,IAAI;YACF,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAClF,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// # sourceMappingURL=proxy-apps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy-apps.js","sourceRoot":"","sources":["../../src/firebase/proxy-apps.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { schemas } from '../index.js';
|
|
2
|
+
import handleModule from './handle-module.js';
|
|
3
|
+
|
|
4
|
+
export default (req, res) => {
|
|
5
|
+
const { method } = req;
|
|
6
|
+
if (method !== 'POST' && method !== 'GET') {
|
|
7
|
+
return res.sendStatus(405);
|
|
8
|
+
}
|
|
9
|
+
if (method === 'POST'
|
|
10
|
+
&& (!req.body || typeof req.body !== 'object' || Array.isArray(req.body))) {
|
|
11
|
+
return res.sendStatus(400);
|
|
12
|
+
}
|
|
13
|
+
let { url } = req;
|
|
14
|
+
if (url.endsWith('.json')) {
|
|
15
|
+
url = url.slice(0, -5);
|
|
16
|
+
}
|
|
17
|
+
[url] = url.split('?');
|
|
18
|
+
const modName = url.split('/')[1];
|
|
19
|
+
const sendSchema = (isResponseSchema = false) => {
|
|
20
|
+
return res.status(200)
|
|
21
|
+
.setHeader('Cache-Control', 'public, max-age=3600')
|
|
22
|
+
.send(schemas[modName][isResponseSchema ? 'response' : 'params']);
|
|
23
|
+
};
|
|
24
|
+
if (modName === '@checkout') {
|
|
25
|
+
if (url === '/@checkout') {
|
|
26
|
+
return res.status(200).send({
|
|
27
|
+
status: 200,
|
|
28
|
+
message: 'CHECKOUT',
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (url === '/@checkout/schema') {
|
|
32
|
+
return sendSchema();
|
|
33
|
+
}
|
|
34
|
+
return res.sendStatus(404);
|
|
35
|
+
}
|
|
36
|
+
if (schemas[modName]) {
|
|
37
|
+
const { params: schema, response: responseSchema } = schemas[modName];
|
|
38
|
+
if (!schema.$schema) {
|
|
39
|
+
schema.$schema = 'http://json-schema.org/draft-07/schema#';
|
|
40
|
+
schema.title = `Module \`${modName}\`: Params model`;
|
|
41
|
+
}
|
|
42
|
+
if (!responseSchema.$schema) {
|
|
43
|
+
responseSchema.$schema = 'http://json-schema.org/draft-07/schema#';
|
|
44
|
+
responseSchema.title = `Module \`${modName}\`: App response model`;
|
|
45
|
+
}
|
|
46
|
+
if (url === `/${modName}/schema`) {
|
|
47
|
+
return sendSchema();
|
|
48
|
+
}
|
|
49
|
+
if (url === `/${modName}/response_schema`) {
|
|
50
|
+
return sendSchema(true);
|
|
51
|
+
}
|
|
52
|
+
if (url === `/${modName}`) {
|
|
53
|
+
return handleModule(modName, schema, responseSchema, req, res)[method]();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return res.sendStatus(404);
|
|
57
|
+
};
|
|
58
|
+
// # sourceMappingURL=serve-modules-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serve-modules-api.js","sourceRoot":"","sources":["../../src/firebase/serve-modules-api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAE3C,eAAe,CAAC,GAAY,EAAE,GAAa,EAAE,EAAE;IAC7C,MAAM,EAAE,MAAM,E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ACD,IAAI,GAAG,KAAK,mBAAmB,EAAE;YAC/B,OAAO,UAAU,EAAE,CAAC;SACrB;QACD,OAAO,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;KAC5B;IAED,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;QACpB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;QACtE,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;YACnB,MAAM,CAAC,OAAO,GAAG,yCAAyC,CAAC;YAC3D,MAAM,CAAC,KAAK,GAAG,YAAY,OAAO,kBAAkB,CAAC;SACtD;QACD,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE;YAC3B,cAAc,CAAC,OAAO,GAAG,yCAAyC,CAAC;YACnE,cAAc,CAAC,KAAK,GAAG,YAAY,OAAO,wBAAwB,CAAC;SACpE;QACD,IAAI,GAAG,KAAK,IAAI,OAAO,SAAS,EAAE;YAChC,OAAO,UAAU,EAAE,CAAC;SACrB;QACD,IAAI,GAAG,KAAK,IAAI,OAAO,kBAAkB,EAAE;YACzC,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC;SACzB;QACD,IAAI,GAAG,KAAK,IAAI,OAAO,EAAE,EAAE;YACzB,OAAO,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;SAC1E;KACF;IACD,OAAO,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;AAC7B,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
import '@cloudcommerce/firebase/lib/init';
|
|
3
|
+
// eslint-disable-next-line import/no-unresolved
|
|
4
|
+
import { onRequest } from 'firebase-functions/v2/https';
|
|
5
|
+
import config from '@cloudcommerce/firebase/lib/config';
|
|
6
|
+
import serveModulesApi from './firebase/serve-modules-api.js';
|
|
7
|
+
|
|
8
|
+
const { httpsFunctionOptions } = config.get();
|
|
9
|
+
|
|
10
|
+
export const modules = onRequest(httpsFunctionOptions, (req, res) => {
|
|
11
|
+
serveModulesApi(req, res);
|
|
12
|
+
});
|
|
13
|
+
// # sourceMappingURL=firebase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"firebase.js","sourceRoot":"","sources":["../src/firebase.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,kCAAkC,CAAC;AAC1C,gDAAgD;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,MAAM,MAAM,oCAAoC,CAAC;AACxD,OAAO,eAAe,MAAM,8BAA8B,CAAC;AAE3D,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,CAAC,GAAG,EAAE,CAAC;AAE9C,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC,oBAAoB,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;IAClE,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAC5B,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as calculateShipping from '../schemas/calculate_shipping.cjs';
|
|
2
|
+
import * as applyDiscount from '../schemas/apply_discount.cjs';
|
|
3
|
+
import * as listPayments from '../schemas/list_payments.cjs';
|
|
4
|
+
import * as createTransaction from '../schemas/create_transaction.cjs';
|
|
5
|
+
import * as checkout from '../schemas/@checkout.cjs';
|
|
6
|
+
|
|
7
|
+
const schemas = {
|
|
8
|
+
calculate_shipping: calculateShipping,
|
|
9
|
+
apply_discount: applyDiscount,
|
|
10
|
+
list_payments: listPayments,
|
|
11
|
+
create_transaction: createTransaction,
|
|
12
|
+
'@checkout': checkout,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default {
|
|
16
|
+
calculateShipping,
|
|
17
|
+
applyDiscount,
|
|
18
|
+
listPayments,
|
|
19
|
+
createTransaction,
|
|
20
|
+
checkout,
|
|
21
|
+
schemas,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { schemas };
|
|
25
|
+
// # sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,iBAAiB,MAAM,mCAAmC,CAAC;AACvE,OAAO,KAAK,aAAa,MAAM,+BAA+B,CAAC;AAC/D,OAAO,KAAK,YAAY,MAAM,8BAA8B,CAAC;AAC7D,OAAO,KAAK,iBAAiB,MAAM,mCAAmC,CAAC;AACvE,OAAO,KAAK,QAAQ,MAAM,0BAA0B,CAAC;AAErD,MAAM,OAAO,GAAG;IACd,kBAAkB,EAAE,iBAAiB;IACrC,cAAc,EAAE,aAAa;IAC7B,aAAa,EAAE,YAAY;IAC3B,kBAAkB,EAAE,iBAAiB;IACrC,WAAW,EAAE,QAAQ;CACtB,CAAC;AAEF,eAAe;IACb,iBAAiB;IACjB,aAAa;IACb,YAAY;IACZ,iBAAiB;IACjB,QAAQ;IACR,OAAO;CACR,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -1,114 +1,110 @@
|
|
|
1
1
|
import { logger } from 'firebase-functions';
|
|
2
2
|
import api from '@cloudcommerce/api';
|
|
3
3
|
import getEnv from '@cloudcommerce/firebase/lib/env';
|
|
4
|
+
|
|
4
5
|
const findCustomerByEmail = async (email) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
catch (e) {
|
|
13
|
-
logger.error(e);
|
|
14
|
-
return null;
|
|
6
|
+
try {
|
|
7
|
+
const { data } = await api.get(`customers?main_email=${email}`);
|
|
8
|
+
if (data.result.length) {
|
|
9
|
+
return data.result[0];
|
|
15
10
|
}
|
|
11
|
+
return null;
|
|
12
|
+
} catch (e) {
|
|
13
|
+
logger.error(e);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
16
|
};
|
|
17
17
|
const checkFirebaseAuth = async (auth, authToken) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return null;
|
|
18
|
+
if (authToken && !Array.isArray(authToken)) {
|
|
19
|
+
try {
|
|
20
|
+
const customer = await auth.verifyIdToken(authToken);
|
|
21
|
+
return customer;
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return null;
|
|
29
24
|
}
|
|
25
|
+
} else {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
30
28
|
};
|
|
31
29
|
const createCustomer = async (customer) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
30
|
+
try {
|
|
31
|
+
const { data } = await api.post('customers', customer);
|
|
32
|
+
return data._id;
|
|
33
|
+
} catch (e) {
|
|
34
|
+
logger.error(e);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
40
37
|
};
|
|
41
38
|
const generateAccessToken = async (firestore, customerId) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
39
|
+
const { apiAuth } = getEnv();
|
|
40
|
+
const docRef = firestore.doc(`customerTokens/${customerId}`);
|
|
41
|
+
const doc = await docRef.get();
|
|
42
|
+
const expires = doc.data()?.expires;
|
|
43
|
+
if (expires && new Date(expires).getTime() - Date.now() >= 2 * 60 * 1000) {
|
|
44
|
+
return doc.data();
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const { data } = await api({
|
|
48
|
+
endpoint: 'authenticate',
|
|
49
|
+
method: 'post',
|
|
50
|
+
body: {
|
|
51
|
+
_id: apiAuth.authenticationId,
|
|
52
|
+
api_key: apiAuth.apiKey,
|
|
53
|
+
customer_id: customerId,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
const accessToken = {
|
|
57
|
+
access_token: data.access_token,
|
|
58
|
+
expires: data.expires,
|
|
59
|
+
customer_id: customerId,
|
|
60
|
+
};
|
|
61
|
+
docRef.set(accessToken).catch(logger.error);
|
|
62
|
+
return accessToken;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
logger.error(e);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
71
67
|
};
|
|
72
68
|
const getAuthCustomerApi = async (firestore, authtoken, authFirebase) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
const newCustomer = {
|
|
80
|
-
display_name: customerFirebaseAuth.name || '',
|
|
81
|
-
main_email: customerFirebaseAuth.email,
|
|
82
|
-
emails: [{
|
|
83
|
-
address: customerFirebaseAuth.email,
|
|
84
|
-
verified: customerFirebaseAuth.email_verified,
|
|
85
|
-
}],
|
|
86
|
-
oauth_providers: [{
|
|
87
|
-
provider: customerFirebaseAuth.firebase.sign_in_provider,
|
|
88
|
-
user_id: customerFirebaseAuth.user_id,
|
|
89
|
-
}],
|
|
90
|
-
};
|
|
91
|
-
const customerId = await createCustomer(newCustomer);
|
|
92
|
-
if (customerId) {
|
|
93
|
-
return generateAccessToken(firestore, customerId);
|
|
94
|
-
}
|
|
69
|
+
const customerFirebaseAuth = await checkFirebaseAuth(authFirebase, authtoken);
|
|
70
|
+
if (customerFirebaseAuth !== null && customerFirebaseAuth.email) {
|
|
71
|
+
const customer = await findCustomerByEmail(customerFirebaseAuth.email);
|
|
72
|
+
if (customer !== null) {
|
|
73
|
+
return generateAccessToken(firestore, customer._id);
|
|
95
74
|
}
|
|
96
|
-
|
|
97
|
-
|
|
75
|
+
const newCustomer = {
|
|
76
|
+
display_name: customerFirebaseAuth.name || '',
|
|
77
|
+
main_email: customerFirebaseAuth.email,
|
|
78
|
+
emails: [{
|
|
79
|
+
address: customerFirebaseAuth.email,
|
|
80
|
+
verified: customerFirebaseAuth.email_verified,
|
|
81
|
+
}],
|
|
82
|
+
oauth_providers: [{
|
|
83
|
+
provider: customerFirebaseAuth.firebase.sign_in_provider,
|
|
84
|
+
user_id: customerFirebaseAuth.user_id,
|
|
85
|
+
}],
|
|
86
|
+
};
|
|
87
|
+
const customerId = await createCustomer(newCustomer);
|
|
88
|
+
if (customerId) {
|
|
89
|
+
return generateAccessToken(firestore, customerId);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// TODO: Find customer by phone number, generate token if found, otherwise unauthorize
|
|
93
|
+
return null;
|
|
98
94
|
};
|
|
99
95
|
const sendError = (res, msg, status = 400) => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
96
|
+
if (msg) {
|
|
97
|
+
res.status(status).json({
|
|
98
|
+
status,
|
|
99
|
+
error: msg,
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
res.status(500).json({
|
|
103
|
+
status: 500,
|
|
104
|
+
error: 'Internal server error',
|
|
105
|
+
});
|
|
106
|
+
}
|
|
112
107
|
};
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
|
|
109
|
+
export { sendError, getAuthCustomerApi };
|
|
110
|
+
// # sourceMappingURL=handle-passport.js.map
|
|
@@ -1,42 +1,41 @@
|
|
|
1
1
|
import { logger } from 'firebase-functions';
|
|
2
|
-
import { sendError, getAuthCustomerApi
|
|
2
|
+
import { sendError, getAuthCustomerApi } from './handle-passport.js';
|
|
3
|
+
|
|
3
4
|
export default async (req, res, firestore, authFirebase, storeId) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const { method } = req;
|
|
6
|
+
if (method !== 'POST') {
|
|
7
|
+
return res.sendStatus(405);
|
|
8
|
+
}
|
|
9
|
+
if (method === 'POST'
|
|
9
10
|
&& (!req.body || typeof req.body !== 'object' || Array.isArray(req.body))) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
return res.sendStatus(400);
|
|
12
|
+
}
|
|
13
|
+
let { url } = req;
|
|
14
|
+
if (url.endsWith('.json')) {
|
|
15
|
+
url = url.slice(0, -5);
|
|
16
|
+
}
|
|
17
|
+
const endpoint = url.split('/')[1];
|
|
18
|
+
// endpoint /token
|
|
19
|
+
if (endpoint === 'token' && method === 'POST') {
|
|
20
|
+
const { authtoken } = req.body;
|
|
21
|
+
if (!firestore) {
|
|
22
|
+
return sendError(res, 'Firestore not found', 500);
|
|
15
23
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (endpoint === 'token' && method === 'POST') {
|
|
19
|
-
const { authtoken } = req.body;
|
|
20
|
-
if (!firestore) {
|
|
21
|
-
return sendError(res, 'Firestore not found', 500);
|
|
22
|
-
}
|
|
23
|
-
if (storeId < 100) {
|
|
24
|
-
return sendError(res, 'Invalid store');
|
|
25
|
-
}
|
|
26
|
-
try {
|
|
27
|
-
const authCustomerApi = await getAuthCustomerApi(firestore, authtoken, authFirebase);
|
|
28
|
-
if (authCustomerApi !== null) {
|
|
29
|
-
return res.send(authCustomerApi);
|
|
30
|
-
}
|
|
31
|
-
return sendError(res, 'Invalid token, unauthorized', 401);
|
|
32
|
-
}
|
|
33
|
-
catch (e) {
|
|
34
|
-
logger.error(e);
|
|
35
|
-
return sendError(res);
|
|
36
|
-
}
|
|
24
|
+
if (storeId < 100) {
|
|
25
|
+
return sendError(res, 'Invalid store');
|
|
37
26
|
}
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
try {
|
|
28
|
+
const authCustomerApi = await getAuthCustomerApi(firestore, authtoken, authFirebase);
|
|
29
|
+
if (authCustomerApi !== null) {
|
|
30
|
+
return res.send(authCustomerApi);
|
|
31
|
+
}
|
|
32
|
+
return sendError(res, 'Invalid token, unauthorized', 401);
|
|
33
|
+
} catch (e) {
|
|
34
|
+
logger.error(e);
|
|
35
|
+
return sendError(res);
|
|
40
36
|
}
|
|
37
|
+
} else {
|
|
38
|
+
return res.sendStatus(400);
|
|
39
|
+
}
|
|
41
40
|
};
|
|
42
|
-
|
|
41
|
+
// # sourceMappingURL=serve-passport-api.js.map
|
|
@@ -7,6 +7,7 @@ import { getAuth } from 'firebase-admin/auth';
|
|
|
7
7
|
import { onRequest } from 'firebase-functions/v2/https';
|
|
8
8
|
import config from '@cloudcommerce/firebase/lib/config';
|
|
9
9
|
import servePassportApi from './firebase/serve-passport-api.js';
|
|
10
|
+
|
|
10
11
|
// References:
|
|
11
12
|
// https:// firebase.google.com/docs/auth/admin/manage-cookies
|
|
12
13
|
// https://itnext.io/how-to-use-firebase-auth-with-a-custom-node-backend-99a106376c8a
|
|
@@ -15,12 +16,13 @@ import servePassportApi from './firebase/serve-passport-api.js';
|
|
|
15
16
|
const authFirebase = getAuth();
|
|
16
17
|
const firestore = getFirestore();
|
|
17
18
|
const options = {
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
...config.get().httpsFunctionOptions,
|
|
20
|
+
memory: '128MiB',
|
|
20
21
|
};
|
|
22
|
+
|
|
21
23
|
// eslint-disable-next-line import/prefer-default-export
|
|
22
24
|
export const passport = onRequest(options, (req, res) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
const { storeId } = config.get();
|
|
26
|
+
servePassportApi(req, res, firestore, authFirebase, storeId);
|
|
25
27
|
});
|
|
26
|
-
|
|
28
|
+
// # sourceMappingURL=firebase.js.map
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
// # sourceMappingURL=index.js.map
|