Skip to content

Commit

Permalink
Fix/utils (#69)
Browse files Browse the repository at this point in the history
* fix:docker-file

* fix: round robin error with medusa

* fix: round-robin

* fix: removed profile relationship

* fix: rate limiting

* fix: docker file temporarily to ignore engines

* fix: Registration

* fix: logging error

* fix: sync fetch products

* fix: made strapi-sync-optional

* fix: updated to optionally initiated sync

* added route to manually trigger sync

---------

Co-authored-by: Govind Diwakar <[email protected]>
  • Loading branch information
SGFGOV and Govind Diwakar authored Nov 21, 2023
1 parent 6bad597 commit 4af57c1
Show file tree
Hide file tree
Showing 21 changed files with 56,817 additions and 1,228 deletions.
56,365 changes: 56,365 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/medusa-plugin-strapi-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ where the strapiOptions will be like
```
const strapiOptions =
{
sync_on_init:true,
encryption_algorithm: "aes-256-cbc",
strapi_protocol: process?.env?.STRAPI_PROTOCOL,
strapi_default_user: {
Expand Down Expand Up @@ -86,6 +87,7 @@ const strapiOptions =
STRAPI_PUBLIC_KEY?: string; - a public key if the secret is not used

#### Advanced options
sync_on_init: initiates synchronisation when the strapi server is bootstrapped. However, this may not be ideal when there is a large number of products, or if you are spinning up multiple containerised deployments to deal with peak load. So its turned off by default. alternatively you can listend for a startup event and trigger executeStrapiSync from your code.
strapi_ignore_threshold: number;
enable_marketplace?: boolean; ## experiment doesn't do anything at the moment
enable_auto_retry?: boolean; ## experiment doesn't do anything at the moment
Expand All @@ -94,6 +96,10 @@ const strapiOptions =
auto_start?: boolean; - starts the interface automatically along with medusa, not recommended as the medusa server may not be ready to sync as soon as strapi is ready
max_page_size?: number;

##### advanced environemental options
STAPI_HOOKS_MAX_REQUESTS - number of requests before hooks ratelimit kicksin
STAPI_HOOKS_MAX_DELAY - number of requests before hooks ratelimit window


## Using Strapi Plugin

Expand Down
6 changes: 3 additions & 3 deletions packages/medusa-plugin-strapi-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medusa-plugin-strapi-ts",
"version": "5.0.5",
"version": "5.0.21",
"description": "A plugin for medusa to use strapi in the backend as the cms in typescript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -33,7 +33,7 @@
"test": "jest --coverage --runInBand --setupFiles=dotenv/config "
},
"devDependencies": {
"@medusajs/medusa": "^1.16.1",
"@medusajs/medusa": "^1.17.4",
"@strapi/plugin-users-permissions": "^4.6.1",
"@types/express": "^4.17.17",
"@types/jest": "^27.4.0",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
"peerDependencies": {
"@medusajs/admin": "^7.1.2",
"@medusajs/medusa": "^1.16.1",
"@medusajs/medusa": "^1.17.4",
"@strapi/plugin-users-permissions": "^4.6.1",
"axios": "^0.27.2",
"medusa-interfaces": "latest",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MedusaError } from '@medusajs/utils';
import {MedusaRequest , MedusaResponse} from "@medusajs/medusa"
import UpdateStrapiService from '../../../services/update-strapi';

export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const updateStrapiService = req.scope.resolve('UpdateStrapiService') as UpdateStrapiService;

if (updateStrapiService.strapiSuperAdminAuthToken) {
try {
await updateStrapiService.executeSync(updateStrapiService.strapiSuperAdminAuthToken);
} catch (e) {
res.sendStatus(500);
return;
//throw new MedusaError(MedusaError.Types.UNAUTHORIZED, e.message, 'Strapi Error');
}
res.sendStatus(200);
} else {
res.status(500).send("Strapi server hasn't been initalised");
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default async (req: Request & { decodedMessage: StrapiSignalInterface },
'tags',
'type',
'collection',
'profile',
//'profile',
];
const regionRelations = ['countries', 'payment_providers', 'fulfillment_providers', 'currency'];
const shippingProfileRelations = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import UpdateMedusaService from '../../../services/update-medusa';
import * as jwt from 'jsonwebtoken';
import { ConfigModule } from '@medusajs/medusa/dist/types/global';
import { ConfigModule, Logger } from '@medusajs/medusa/dist/types/global';
import { StrapiSignalInterface } from './strapi-signal';

export interface UpdateMedusaDataInterface {
type: string;
data: any;
origin: 'strapi' | 'medusa';
}

export default async (req, res, next) => {
const config = req.scope.resolve('configModule') as ConfigModule;
const updateMedusaService = req.scope.resolve('updateMedusaService') as UpdateMedusaService;

const logger = req.scope.resolve('logger') as Logger;
try {
const medusaSecret = config.projectConfig.jwt_secret;
const signedMessage = req.body['signedMessage'];
Expand All @@ -20,9 +21,16 @@ export default async (req, res, next) => {

// find Strapi entry type from body of webhook
const strapiType = body.type;
const origin = body.origin;
// get the ID
let entryId: string;

if (origin == 'medusa') {
res.sendStatus(200);
logger.info('received update confirmation');
return;
}

let updated = {};
switch (strapiType) {
case 'product':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export default (app, options, config: ConfigModule) => {
/*app.set('query parser', (queryString) => {
return new URLSearchParams(queryString);
});*/
contentRouter.options('/:type/:id', (req, res, next) => {
res.setHeader('Allow', 'GET').sendStatus(200);
});
contentRouter.options('/:type', (req, res, next) => {
res.setHeader('Allow', 'GET').sendStatus(200);
//next();
});
contentRouter.get('/:type/:id', fetchContent);
contentRouter.get('/:type', fetchContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import strapiSignal from '../../controllers/hooks/strapi-signal';
import rateLimiter from 'express-rate-limit';

const limiter = rateLimiter({
max: 5,
windowMs: 10000, // 10 seconds
max: parseInt(process.env.STAPI_HOOKS_MAX_REQUESTS ?? '100') || 100,
windowMs: parseInt(process.env.STAPI_HOOKS_MAX_DELAY ?? '100000') || 100000, // 100 seconds
message: "You can't make any more requests at the moment. Try again later",
});
const hooksRouter = Router();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default (app: Router, options: StrapiMedusaPluginOptions, config: ConfigM
adminRouter.get('/login', async (req: Request, res: Response) => {
const userService = req.scope.resolve('userService') as UserService;
try {
const user = await userService.retrieve(req.user.userId);
const user = await userService.retrieve(req.cookies.ajs_user_id);
delete user.password_hash;
const signedCookie = jwt.sign(JSON.stringify(user), jwtSecret);
res.cookie('__medusa_session', signedCookie);
Expand Down
36 changes: 7 additions & 29 deletions packages/medusa-plugin-strapi-ts/src/services/update-medusa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ class UpdateMedusaService extends TransactionBaseService {
const result = await this.atomicPhase_(async (manager) => {
try {
const product = await this.productService_.withTransaction(manager).retrieve(productId);

if (product.handle.toLowerCase().trim() != productEntry.handle.toLowerCase().trim()) {
this.logger.error(`handle and id mismatch in strapi, `);
throw new Error(
'Synchronization Error - handles mismatched, ' +
'please resync with strapi after dumping strapi database'
);
}
const update = {};
this.logger.debug('old data in medusa : ' + JSON.stringify(product));
this.logger.debug('data received from strapi : ' + JSON.stringify(productEntry));
Expand All @@ -93,34 +99,6 @@ class UpdateMedusaService extends TransactionBaseService {
}
}

// update Medusa product with Strapi product fields
/*const title = productEntry.title;
const subtitle = productEntry.subtitle;
const description = productEntry.description;
const handle = productEntry.handle;
if (product.title !== title) {
update['title'] = title;
}
if (product.subtitle !== subtitle) {
update['subtitle'] = subtitle;
}
if (product.description !== description) {
update['description'] = description;
}
if (product.handle !== handle) {
update['handle'] = handle;
}
// Get the thumbnail, if present
if (productEntry.thumbnail) {
const thumb = null;
update['thumbnail'] = thumb;
}*/

if (!isEmptyObject(update)) {
await this.productService_
.withTransaction(manager)
Expand Down
18 changes: 16 additions & 2 deletions packages/medusa-plugin-strapi-ts/src/services/update-strapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class UpdateStrapiService extends TransactionBaseService {
logger: Logger;
static isHealthy: boolean;
lastAdminLoginAttemptTime: number;
isStarted: boolean;
static isServiceAccountRegistered: boolean;
productCollectionService: ProductCollectionService;
productCategoryService: any;
private enableAdminDataLogging: boolean;
Expand Down Expand Up @@ -282,6 +282,7 @@ export class UpdateStrapiService extends TransactionBaseService {
try {
const result = await this.intializeServer();
this.strapiPluginLog('info', 'Successfully Bootstrapped the strapi server');
UpdateStrapiService.isServiceAccountRegistered = true;
return result;
} catch (e) {
this.strapiPluginLog(
Expand All @@ -293,6 +294,13 @@ export class UpdateStrapiService extends TransactionBaseService {
}
}

async waitForServiceAccountCreation() {
if (process.env.NODE_ENV != 'test')
while (!UpdateStrapiService.isServiceAccountRegistered) {
await sleep(3000);
}
}

async addIgnore_(id, side): Promise<any> {
const key = `${id}_ignore_${side}`;
return await this.redis_.set(key, 1, 'EX', this.options_.strapi_ignore_threshold || IGNORE_THRESHOLD);
Expand Down Expand Up @@ -1428,6 +1436,7 @@ export class UpdateStrapiService extends TransactionBaseService {
...this.options_.strapi_default_user,
};
const registerResponse = await this.executeRegisterMedusaUser(authParams);
UpdateStrapiService.isServiceAccountRegistered = true;
return registerResponse?.data;
} catch (error) {
this.strapiPluginLog('error', 'unable to register default user', { error: (error as Error).message });
Expand Down Expand Up @@ -1580,6 +1589,7 @@ export class UpdateStrapiService extends TransactionBaseService {
}
): Promise<AxiosResponse> {
await this.waitForHealth();
await this.waitForServiceAccountCreation();
try {
const authData = {
identifier: authInterface.email.toLowerCase(),
Expand Down Expand Up @@ -1937,6 +1947,7 @@ export class UpdateStrapiService extends TransactionBaseService {
}): Promise<AxiosResponse> {
let endPoint: string = undefined;
await this.waitForHealth();
await this.waitForServiceAccountCreation();
let tail = '';
// if (method.toLowerCase() != 'post') {
if (method.toLowerCase() != 'post') {
Expand Down Expand Up @@ -2340,7 +2351,7 @@ export class UpdateStrapiService extends TransactionBaseService {

this.strapiPluginLog('info', 'Logged In Admin ' + auth.email + ' with strapi');
this.strapiPluginLog('info', 'Admin profile', response.data.data.user);
this.strapiPluginLog('info', 'Admin token', response.data.data.token);
//this.strapiPluginLog('info', 'Admin token', response.data.data.token);

this.strapiSuperAdminAuthToken = response.data.data.token;
this.userAdminProfile = response.data.data.user;
Expand All @@ -2362,6 +2373,9 @@ export class UpdateStrapiService extends TransactionBaseService {
await this.registerOrLoginAdmin();
if (this.strapiSuperAdminAuthToken) {
const user = (await this.registerOrLoginDefaultMedusaUser()).user;
if (!this.options_.sync_on_init) {
return { status: 200 };
}
if (user) {
const response = await this.executeSync(this.strapiSuperAdminAuthToken);
/* const response = await this.configureStrapiMedusaForUser({
Expand Down
1 change: 1 addition & 0 deletions packages/medusa-plugin-strapi-ts/src/types/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Method } from 'axios';
import { BaseEntity } from '@medusajs/medusa';

export interface StrapiMedusaPluginOptions {
sync_on_init?: boolean;
enable_auto_retry?: boolean;
encryption_algorithm: string;
strapi_protocol: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa-strapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ENV NODE_ENV development
COPY ./public ./public
COPY ./favicon.ico ./favicon.ico

RUN yarn install --ignore-platform --network-timeout 10000000 --force && yarn build
RUN yarn install --ignore-platform --network-timeout 10000000 --force --ignore-engines && yarn build


EXPOSE 1337
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa-strapi/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ COPY ./package.json ./
COPY ./src ./src
COPY ./public ./public
COPY ./favicon.ico ./favicon.ico
RUN yarn config set network-timeout 600000 -g && yarn install --production
RUN yarn config set network-timeout 600000 -g && yarn install --production --ignore-engines
RUN yarn build


Expand Down
3 changes: 1 addition & 2 deletions packages/medusa-strapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"sentry": "^0.1.2",
"sharp": "^0.32.1",
"strapi-google-translator": "^1.0.7",
"strapi-html-editor": "^1.0.2",
"strapi-middleware-cache": "^2.1.8",
"strapi-plugin-comments": "^2.2.3",
"strapi-plugin-country-select": "^1.0.0",
Expand Down Expand Up @@ -126,4 +125,4 @@
"**/word-wrap": "^1.2.4",
"**/fast-xml-parser": "^4.2.4"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@
const axios = require('axios');

module.exports = {
async afterUpdate(result, params, data) {
async beforeUpdate({ params, state, action, model }) {
if (params.data.updateFrom == 'medusa') state.updateFrom = 'medusa';
},

async afterUpdate({ params, state, result, model, action }) {
let medusaReady = false;
/* while (!medusaReady) {
try {
const response = await axios.head(
`${process.env.MEDUSA_BACKEND_URL}/health`
);
medusaReady = response.status < 300 ? true : false;
} catch (e) {
console.log("awaiting medusa to start");
return;
}
}*/
/*while (!medusaReady) {
try {
const response = await axios.head(`${process.env.MEDUSA_BACKEND_URL}/health`);
medusaReady = response.status < 300 ? true : false;
} catch (e) {
handleError(strapi, e);
return;
}
}*/
const origin = state.updateFrom ?? 'strapi';
const respondViaPlugin = strapi.plugins['strapi-plugin-medusajs'].service('setup');
return await respondViaPlugin.sendResult('productVariant', result.result);
/*
return await respondViaPlugin.sendResult('productVariant', result, origin); /* await axios.post(
/*
await axios.post(
`${
process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const handleError = require('../../../../utils/utils').handleError;
const axios = require('axios');

module.exports = {
async afterUpdate(result, params, data) {
async beforeUpdate({ params, state, action, model }) {
if (params.data.updateFrom == 'medusa') state.updateFrom = 'medusa';
},

async afterUpdate({ params, state, result, model, action }) {
let medusaReady = false;
/*while (!medusaReady) {
try {
Expand All @@ -19,9 +23,9 @@ module.exports = {
return;
}
}*/

const origin = state.updateFrom ?? 'strapi';
const respondViaPlugin = strapi.plugins['strapi-plugin-medusajs'].service('setup');
return await respondViaPlugin.sendResult('product', result.result); /* await axios.post(
return await respondViaPlugin.sendResult('product', result, origin); /* await axios.post(
`${
process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"
}/hooks/strapi/update-medusa`,
Expand Down
Loading

0 comments on commit 4af57c1

Please sign in to comment.