Skip to content

Commit

Permalink
Fix/updates (#64)
Browse files Browse the repository at this point in the history
* fix: multicountry dependency

* feat: auto embed admin module

* fix: single admin

* fix: has types check removed

* fix: tests

* fix: logic flow in case of unfound entity

* fix: tests
* fix: updated data flow, tests and performance, the module now supports automatic entity creation

---------

Co-authored-by: Govind Diwakar <sgf@sourcegoodfood.com>
  • Loading branch information
SGFGOV and Govind Diwakar authored Oct 4, 2023
1 parent 9cf6c5f commit 6bad597
Show file tree
Hide file tree
Showing 16 changed files with 5,791 additions and 47,184 deletions.
45,212 changes: 0 additions & 45,212 deletions package-lock.json

This file was deleted.

11 changes: 6 additions & 5 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.4",
"version": "5.0.5",
"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 All @@ -19,8 +19,8 @@
"e-commerce",
"content management"
],
"funding":{
"type":"github",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/SGFGOV"
},
"author": "Govind Diwakar",
Expand All @@ -33,7 +33,7 @@
"test": "jest --coverage --runInBand --setupFiles=dotenv/config "
},
"devDependencies": {
"@medusajs/medusa": "^1.8.2",
"@medusajs/medusa": "^1.16.1",
"@strapi/plugin-users-permissions": "^4.6.1",
"@types/express": "^4.17.17",
"@types/jest": "^27.4.0",
Expand Down Expand Up @@ -67,7 +67,8 @@
"typescript": "^4.9.5"
},
"peerDependencies": {
"@medusajs/medusa": "^1.8.2",
"@medusajs/admin": "^7.1.2",
"@medusajs/medusa": "^1.16.1",
"@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
@@ -1,14 +1,13 @@
import UpdateMedusaService from '../../../services/update-medusa';
import * as jwt from "jsonwebtoken";
import {ConfigModule} from "@medusajs/medusa/dist/types/global";
import {StrapiSignalInterface} from "./strapi-signal";
import * as jwt from 'jsonwebtoken';
import { ConfigModule } from '@medusajs/medusa/dist/types/global';
import { StrapiSignalInterface } from './strapi-signal';

export interface UpdateMedusaDataInterface {
type: string;
data: any;
}


export default async (req, res, next) => {
const config = req.scope.resolve('configModule') as ConfigModule;
const updateMedusaService = req.scope.resolve('updateMedusaService') as UpdateMedusaService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import cors from 'cors';

import authenticate from '@medusajs/medusa/dist/api/middlewares/authenticate';
import { StrapiMedusaPluginOptions } from '../../../types/globals';
import { ConfigModule } from '@medusajs/medusa/dist/types/global';
import { ConfigModule } from '@medusajs/types';
import { UserService } from '@medusajs/medusa';

const adminRouter = Router();
Expand All @@ -29,7 +29,7 @@ export default (app: Router, options: StrapiMedusaPluginOptions, config: ConfigM
if (process.env.NODE_ENV != 'test') {
adminRouter.use(cors(adminCors));
}
const jwtSecret = config.projectConfig.jwt_secret;
const jwtSecret = config.projectConfig.jwt_secret || (config.projectConfig as any).jwtSecret;
adminRouter.options('/login', cors(adminCors));
adminRouter.get('/login', cors(adminCors));
adminRouter.get('/login', authenticate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ import { IdMap, MockManager } from 'medusa-test-utils';
import UpdateStrapiService, { StrapiResult } from '../update-strapi';
import logger from '../__mocks__/logger';
import axios, { AxiosError } from 'axios';
import exp from 'constants';
import qs from 'qs';

// This sets the mock adapter on the default instance

let service: StrapiService;
let result: StrapiResult;
const testTimeOut = 120e3;
jest.setTimeout(testTimeOut);
const testBaseTimeOut = 20e3;
jest.setTimeout(testBaseTimeOut);

describe('StrapiService Tests', () => {
jest.setTimeout(testTimeOut);
jest.setTimeout(testBaseTimeOut);
try {
axios.head(`${strapiPath}/_health`).then(
() => disableMocks(),
Expand Down Expand Up @@ -159,7 +157,7 @@ describe('StrapiService Tests', () => {
const roleId = await service.getRoleId('Author');
expect(roleId).toBeGreaterThan(0);
},
testTimeOut
testBaseTimeOut * 5
);

it(
Expand Down Expand Up @@ -191,7 +189,7 @@ describe('StrapiService Tests', () => {
expect(1).toBe(1);
}
},
testTimeOut
testBaseTimeOut
);

it(
Expand All @@ -211,7 +209,7 @@ describe('StrapiService Tests', () => {
expect(1).toBe(1);
}
},
testTimeOut
testBaseTimeOut
);

it(
Expand All @@ -220,7 +218,7 @@ describe('StrapiService Tests', () => {
const roleId = await service.getRoleId('new role');
expect(roleId).toBe(-1);
},
testTimeOut
testBaseTimeOut
);

it(
Expand All @@ -229,7 +227,7 @@ describe('StrapiService Tests', () => {
const credentials = await service.registerOrLoginDefaultMedusaUser();
expect(credentials.token).toBeDefined();
},
testTimeOut
testBaseTimeOut
);

it(
Expand All @@ -244,7 +242,7 @@ describe('StrapiService Tests', () => {
expect(true).toBe(true);
}
},
testTimeOut
testBaseTimeOut
);
});
describe('user actions and signal send', () => {
Expand All @@ -262,7 +260,7 @@ describe('StrapiService Tests', () => {
expect(result.status).toBeGreaterThanOrEqual(200);
expect(result.status).toBeLessThan(300);
},
testTimeOut
testBaseTimeOut * 3
);
});

Expand All @@ -273,7 +271,7 @@ describe('StrapiService Tests', () => {
await service.deleteDefaultMedusaUser();
});

const spy = jest.spyOn(service, 'getType');
// const spyGetType = jest.spyOn(service, 'getType');
describe('product creation and update', () => {
it('product-type-creation', async () => {
const typeResult = await service.createProductTypeInStrapi('dummy', defaultAuthInterface);
Expand All @@ -286,7 +284,7 @@ describe('StrapiService Tests', () => {
expect(typeGetResult).toBeDefined();
expect(typeGetResult.data.length > 0).toBeTruthy();
}
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
});

it(
Expand All @@ -300,7 +298,7 @@ describe('StrapiService Tests', () => {
expect(result.data).toMatchObject({
medusa_id: product_id,
});
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
if (result) {
const productGetResult = await service.getEntitiesFromStrapi({
authInterface: defaultAuthInterface,
Expand All @@ -310,7 +308,7 @@ describe('StrapiService Tests', () => {
expect(productGetResult.data.length > 0).toBeTruthy();
}
},
testTimeOut
testBaseTimeOut
);

it(
Expand All @@ -332,7 +330,7 @@ describe('StrapiService Tests', () => {
expect(result.data?.['product_variants']).toBeDefined();
expect(result.data?.['product_variants'].length > 0).toBeTruthy();
}
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
if (result) {
const productGetResult = await service.getEntitiesFromStrapi({
authInterface: defaultAuthInterface,
Expand Down Expand Up @@ -364,7 +362,7 @@ describe('StrapiService Tests', () => {
}
},

testTimeOut
testBaseTimeOut * 3
);

it('product pagination', async () => {
Expand Down Expand Up @@ -401,7 +399,7 @@ describe('StrapiService Tests', () => {
expect(result.data).toMatchObject({
medusa_id: IdMap.getId('exists'),
});
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
if (result) {
const productGetResult = await service.getEntitiesFromStrapi({
authInterface: defaultAuthInterface,
Expand All @@ -415,7 +413,7 @@ describe('StrapiService Tests', () => {
expect(true).toBe(true);
}
},
testTimeOut
testBaseTimeOut
);

it(
Expand All @@ -431,7 +429,7 @@ describe('StrapiService Tests', () => {
medusa_id: IdMap.getId('exists-2'),
});

expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
if (result) {
const productGetResult = await service.getEntitiesFromStrapi({
authInterface: defaultAuthInterface,
Expand All @@ -445,7 +443,7 @@ describe('StrapiService Tests', () => {
expect(true).toBe(true);
}
},
testTimeOut
testBaseTimeOut
);

it(
Expand All @@ -463,9 +461,9 @@ describe('StrapiService Tests', () => {
});
}

expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
},
testTimeOut
testBaseTimeOut * 3
);
it(
'attempt to update non existing product',
Expand All @@ -476,15 +474,15 @@ describe('StrapiService Tests', () => {
);
expect(result).toBeDefined();
expect(result.status == 200 || result.status == 302).toBeTruthy();

if (!isMockEnabled()) {
expect(result).toMatchObject({
data: { title: 'test-product', medusa_id: IdMap.getId('exists-4') },
});
/** as it will automatically create entities that doesn't exist assuming they exist in medusa */
expect(result.data.medusa_id != IdMap.getId('exists-4')).toBeTruthy();
}

expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
},
testTimeOut
testBaseTimeOut * 3
);
it('create metafields in strapi', async () => {
result = await service.createProductMetafieldInStrapi(
Expand All @@ -496,7 +494,7 @@ describe('StrapiService Tests', () => {
);
expect(result).toBeDefined();
expect(result.status == 200 || result.status == 302).toBeTruthy();
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
if (result) {
const productMetafieldsGetResult = await service.getEntitiesFromStrapi({
authInterface: defaultAuthInterface,
Expand All @@ -517,7 +515,7 @@ describe('StrapiService Tests', () => {
);
expect(result).toBeDefined();
expect(result.status).toBe(200);
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
});

it('testing health-check-fail recovery', async () => {
Expand All @@ -531,7 +529,7 @@ describe('StrapiService Tests', () => {
);
expect(result).toBeDefined();
expect(result.status).toBe(200);
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
});

it('create and update product variant in strapi', async () => {
Expand All @@ -554,7 +552,7 @@ describe('StrapiService Tests', () => {
);
expect(result).toBeDefined();
expect(result.status == 200 || result.status == 302).toBeTruthy();
expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
});

it('create and update product category in strapi', async () => {
Expand All @@ -578,7 +576,7 @@ describe('StrapiService Tests', () => {
expect(result).toBeDefined();
expect(result.status == 200 || result.status == 302).toBeTruthy();

expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
});
});

Expand Down Expand Up @@ -673,7 +671,7 @@ describe('StrapiService Tests', () => {
expect(result).toBeDefined();
expect(result.status == 200 || result.status == 302).toBeTruthy();

expect(spy).toHaveBeenCalled();
// expect(spyGetType).toHaveBeenCalled()
if (!isMockEnabled()) {
result = await service.deleteProductInStrapi({ id: IdMap.getId('exists') }, defaultAuthInterface);
const productVariantGetResult = await service.getEntitiesFromStrapi({
Expand Down Expand Up @@ -814,7 +812,7 @@ describe('region checks', () => {
})) as StrapiResult;
expect(result.status).toBe(200);
},
testTimeOut
testBaseTimeOut
);
});
describe('admin CURD', () => {
Expand All @@ -841,7 +839,7 @@ describe('admin CURD', () => {
// expect(result.id).toBeDefined();
// expect(result.id).toBeGreaterThan(0);
},
testTimeOut
testBaseTimeOut
);
it(
'find all admin users',
Expand All @@ -852,7 +850,7 @@ describe('admin CURD', () => {
// expect(result.id).toBeDefined();
// expect(result.id).toBeGreaterThan(0);
},
testTimeOut
testBaseTimeOut
);
if (!isMockEnabled()) {
it(
Expand All @@ -862,7 +860,7 @@ describe('admin CURD', () => {
expect(result).toBeDefined();
expect(result.status == 201 || result.status == 200).toBeTruthy();
},
testTimeOut
testBaseTimeOut
);
}
if (!isMockEnabled()) {
Expand All @@ -872,7 +870,7 @@ describe('admin CURD', () => {
result = await service.registerAdminUserInStrapi('testAuthor@test.com', 'Editor');
expect(result.status == 201 || result.status == 200).toBeTruthy();
},
testTimeOut
testBaseTimeOut
);
}
it(
Expand All @@ -882,7 +880,7 @@ describe('admin CURD', () => {
expect(result).toBeDefined();
expect(result.status == 201 || result.status == 200).toBeTruthy();
},
testTimeOut
testBaseTimeOut
);
if (!isMockEnabled())
it(
Expand All @@ -902,6 +900,6 @@ describe('admin CURD', () => {
expect(result.status).toBe(200);
expect(result.data).toBeUndefined();
},
testTimeOut
testBaseTimeOut
);
});
Loading

0 comments on commit 6bad597

Please sign in to comment.