Clean up unnecessary code

This commit is contained in:
ivanThePleasant 2022-09-29 14:03:41 +03:00
parent 5a4362faf7
commit 01bb760793
18 changed files with 50 additions and 131 deletions

View File

@ -92,14 +92,7 @@ module.exports = {
return ctx.notFound('role.notFound');
}
const { permissions, shouldSendUpdate } = await roleService.assignPermissions(
role.id,
input.permissions
);
if (shouldSendUpdate) {
await getService('metrics').sendDidUpdateRolePermissions(ctx.state?.user);
}
const permissions = await roleService.assignPermissions(role.id, input.permissions);
const sanitizedPermissions = permissions.map(permissionService.sanitizePermission);

View File

@ -82,21 +82,10 @@ describe('Role controller', () => {
test('Fails on missing permissions input', async () => {
const findOne = jest.fn(() => Promise.resolve({ id: 1 }));
const state = {
user: {
id: 1,
},
};
const ctx = createContext(
{
params: { id: 1 },
body: {},
},
{
state,
}
);
const ctx = createContext({
params: { id: 1 },
body: {},
});
global.strapi = {
admin: {
@ -124,23 +113,12 @@ describe('Role controller', () => {
test('Fails on missing action permission', async () => {
const findOne = jest.fn(() => Promise.resolve({ id: 1 }));
const state = {
user: {
id: 1,
const ctx = createContext({
params: { id: 1 },
body: {
permissions: [{}],
},
};
const ctx = createContext(
{
params: { id: 1 },
body: {
permissions: [{}],
},
},
{
state,
}
);
});
global.strapi = {
admin: {
services: {
@ -167,10 +145,7 @@ describe('Role controller', () => {
test('Assign permissions if input is valid', async () => {
const roleID = 1;
const findOneRole = jest.fn(() => Promise.resolve({ id: roleID }));
const assignPermissions = jest.fn((roleID, permissions) =>
Promise.resolve({ permissions, shouldSendUpdate: true })
);
const sendDidUpdateRolePermissions = jest.fn();
const assignPermissions = jest.fn((roleID, permissions) => Promise.resolve(permissions));
const inputPermissions = [
{
action: 'test',
@ -180,24 +155,12 @@ describe('Role controller', () => {
},
];
const state = {
user: {
id: 1,
email: 'someTestEmailString',
const ctx = createContext({
params: { id: roleID },
body: {
permissions: inputPermissions,
},
};
const ctx = createContext(
{
params: { id: roleID },
body: {
permissions: inputPermissions,
},
},
{
state,
}
);
});
global.strapi = {
admin: {
@ -221,9 +184,6 @@ describe('Role controller', () => {
})),
},
},
metrics: {
sendDidUpdateRolePermissions,
},
},
},
};

View File

@ -15,12 +15,7 @@ describe('User Controller', () => {
test('Fails if user already exist', async () => {
const exists = jest.fn(() => Promise.resolve(true));
const state = {
user: {
id: 1,
},
};
const ctx = createContext({ body }, { state });
const ctx = createContext({ body });
global.strapi = {
admin: {
@ -49,15 +44,8 @@ describe('User Controller', () => {
const exists = jest.fn(() => Promise.resolve(false));
const sanitizeUser = jest.fn((user) => Promise.resolve(user));
const created = jest.fn();
const sendDidInviteUser = jest.fn();
const state = {
user: {
id: 1,
email: 'someTestEmailString',
},
};
const ctx = createContext({ body }, { state, created });
console.log(ctx);
const ctx = createContext({ body }, { created });
global.strapi = {
admin: {
services: {
@ -66,9 +54,6 @@ describe('User Controller', () => {
create,
sanitizeUser,
},
metrics: {
sendDidInviteUser,
},
},
},
};
@ -79,7 +64,6 @@ describe('User Controller', () => {
expect(create).toHaveBeenCalledWith(body);
expect(sanitizeUser).toHaveBeenCalled();
expect(created).toHaveBeenCalled();
expect(sendDidInviteUser).toHaveBeenCalled();
});
});

View File

@ -132,11 +132,7 @@ module.exports = {
permissionsToAssign = input.permissions;
}
const { permissions, shouldSendUpdate } = await assignPermissions(role.id, permissionsToAssign);
if (shouldSendUpdate) {
await getService('metrics').sendDidUpdateRolePermissions(ctx.state?.user);
}
const permissions = await assignPermissions(role.id, permissionsToAssign);
ctx.body = {
data: permissions.map(sanitizePermission),

View File

@ -34,8 +34,6 @@ module.exports = {
const createdUser = await getService('user').create(attributes);
getService('metrics').sendDidInviteUser(ctx.state?.user);
const userInfo = getService('user').sanitizeUser(createdUser);
// Note: We need to assign manually the registrationToken to the

View File

@ -28,6 +28,7 @@ describe('User', () => {
describe('create', () => {
const count = jest.fn(() => Promise.resolve(1));
const sendDidInviteUser = jest.fn();
test('Creates a user by merging given and default attributes', async () => {
const create = jest.fn(({ data }) => Promise.resolve(data));
@ -40,6 +41,7 @@ describe('User', () => {
token: { createToken },
auth: { hashPassword },
role: { count },
metrics: { sendDidInviteUser },
},
},
query() {
@ -68,6 +70,7 @@ describe('User', () => {
token: { createToken },
auth: { hashPassword },
role: { count },
metrics: { sendDidInviteUser },
},
},
query() {
@ -109,6 +112,7 @@ describe('User', () => {
token: { createToken },
auth: { hashPassword },
role: { count },
metrics: { sendDidInviteUser },
},
},
query() {

View File

@ -315,7 +315,6 @@ const assignPermissions = async (roleId, permissions = []) => {
const superAdmin = await getService('role').getSuperAdmin();
const isSuperAdmin = superAdmin && superAdmin.id === roleId;
const assignRole = set('role', roleId);
let shouldSendUpdate = false;
const permissionsWithRole = permissions
// Add the role attribute to every permission
@ -352,13 +351,10 @@ const assignPermissions = async (roleId, permissions = []) => {
}
if (!isSuperAdmin && (permissionsToAdd.length || permissionsToDelete.length)) {
shouldSendUpdate = true;
await getService('metrics').sendDidUpdateRolePermissions();
}
return {
permissions: permissionsToReturn,
shouldSendUpdate,
};
return permissionsToReturn;
};
const addPermissions = async (roleId, permissions) => {

View File

@ -41,6 +41,8 @@ const create = async (attributes) => {
const createdUser = await strapi.query('admin::user').create({ data: user, populate: ['roles'] });
getService('metrics').sendDidInviteUser();
return createdUser;
};

View File

@ -105,7 +105,7 @@ module.exports = {
const newConfiguration = await contentTypeService.updateConfiguration(contentType, input);
await metricsService.sendDidConfigureListView(contentType, newConfiguration, ctx.state?.user);
await metricsService.sendDidConfigureListView(contentType, newConfiguration);
const confWithUpdatedMetadata = {
...newConfiguration,

View File

@ -57,7 +57,7 @@ module.exports = function createComponentBuilder() {
.set('config', infos.config)
.setAttributes(this.convertAttributes(infos.attributes));
if (strapi.components.size === 0) {
if (this.components.size === 0) {
strapi.telemetry.send('didCreateFirstComponent');
} else {
strapi.telemetry.send('didCreateComponent');

View File

@ -40,7 +40,7 @@ const createTelemetryInstance = (strapi) => {
register() {
if (!isDisabled) {
const pingCron = scheduleJob('0 0 12 * * *', () => sendEvent('', 'ping'));
const pingCron = scheduleJob('0 0 12 * * *', () => sendEvent('ping'));
crons.push(pingCron);
strapi.server.use(createMiddleware({ sendEvent }));

View File

@ -231,7 +231,7 @@ module.exports = {
});
strapi.telemetry.send('didBulkMoveMediaLibraryElements', {
groupProperties: {
eventProperties: {
rootFolderNumber: updatedFolders.length,
rootAssetNumber: updatedFiles.length,
totalFolderNumber,

View File

@ -19,12 +19,6 @@ module.exports = {
await getService('upload').setSettings(data);
if (data.responsiveDimensions === true) {
strapi.telemetry.send('didEnableResponsiveDimensions');
} else {
strapi.telemetry.send('didDisableResponsiveDimensions');
}
ctx.body = { data };
},

View File

@ -341,7 +341,7 @@ module.exports = ({ strapi }) => ({
fileValues[UPDATED_BY_ATTRIBUTE] = user.id;
}
sendMediaMetrics(fileValues, user);
sendMediaMetrics(fileValues);
const res = await strapi.entityService.update(FILE_MODEL_UID, id, { data: fileValues });
@ -357,7 +357,7 @@ module.exports = ({ strapi }) => ({
fileValues[CREATED_BY_ATTRIBUTE] = user.id;
}
sendMediaMetrics(fileValues, user);
sendMediaMetrics(fileValues);
const res = await strapi.query(FILE_MODEL_UID).create({ data: fileValues });
@ -442,6 +442,12 @@ module.exports = ({ strapi }) => ({
},
setSettings(value) {
if (value.responsiveDimensions === true) {
strapi.telemetry.send('didEnableResponsiveDimensions');
} else {
strapi.telemetry.send('didDisableResponsiveDimensions');
}
return strapi.store({ type: 'plugin', name: 'upload', key: 'settings' }).set({ value });
},
});

View File

@ -48,7 +48,6 @@ describe('Locales', () => {
const expectedLocales = { code: 'af', name: 'Afrikaans (af)', isDefault: true };
const getDefaultLocale = jest.fn(() => Promise.resolve('af'));
const setDefaultLocale = jest.fn(() => Promise.resolve());
const sendDidUpdateI18nLocalesEvent = jest.fn(() => Promise.resolve());
const setIsDefault = jest.fn(() => expectedLocales);
const findByCode = jest.fn(() => undefined);
@ -66,9 +65,6 @@ describe('Locales', () => {
setDefaultLocale,
create,
},
metrics: {
sendDidUpdateI18nLocalesEvent,
},
},
},
},
@ -89,7 +85,6 @@ describe('Locales', () => {
const locale = { code: 'af', name: 'Afrikaans (af)' };
const expectedLocale = { code: 'af', name: 'Afrikaans (af)', isDefault: false };
const getDefaultLocale = jest.fn(() => Promise.resolve('en'));
const sendDidUpdateI18nLocalesEvent = jest.fn(() => Promise.resolve());
const setIsDefault = jest.fn(() => expectedLocale);
const findByCode = jest.fn(() => undefined);
@ -106,9 +101,6 @@ describe('Locales', () => {
getDefaultLocale,
create,
},
metrics: {
sendDidUpdateI18nLocalesEvent,
},
},
},
},
@ -179,7 +171,6 @@ describe('Locales', () => {
const updates = { name: 'Afrikaans' };
const expectedLocales = { code: 'af', name: 'Afrikaans', isDefault: true };
const setDefaultLocale = jest.fn(() => Promise.resolve());
const sendDidUpdateI18nLocalesEvent = jest.fn(() => Promise.resolve());
const setIsDefault = jest.fn(() => expectedLocales);
const findById = jest.fn(() => existingLocale);
@ -196,9 +187,6 @@ describe('Locales', () => {
setDefaultLocale,
update,
},
metrics: {
sendDidUpdateI18nLocalesEvent,
},
},
},
},
@ -274,7 +262,6 @@ describe('Locales', () => {
const locale = { code: 'af', name: 'Afrikaans (af)' };
const expectedLocales = { code: 'af', name: 'Afrikaans (af)', isDefault: false };
const getDefaultLocale = jest.fn(() => Promise.resolve('en'));
const sendDidUpdateI18nLocalesEvent = jest.fn(() => Promise.resolve());
const setIsDefault = jest.fn(() => expectedLocales);
const findById = jest.fn(() => locale);
@ -291,9 +278,6 @@ describe('Locales', () => {
getDefaultLocale,
delete: deleteFn,
},
metrics: {
sendDidUpdateI18nLocalesEvent,
},
},
},
},
@ -334,7 +318,7 @@ describe('Locales', () => {
sanitizers,
};
const ctx = { params: { id: 1 }, state: { user: { id: 1 } } };
const ctx = { params: { id: 1 } };
expect.assertions(5);

View File

@ -43,8 +43,6 @@ module.exports = {
const locale = await localesService.create(localeToPersist);
getService('metrics').sendDidUpdateI18nLocalesEvent(user);
if (isDefault) {
await localesService.setDefaultLocale(locale);
}
@ -74,8 +72,6 @@ module.exports = {
const updatedLocale = await localesService.update({ id }, cleanUpdates);
getService('metrics').sendDidUpdateI18nLocalesEvent(user);
if (isDefault) {
await localesService.setDefaultLocale(updatedLocale);
}
@ -102,8 +98,6 @@ module.exports = {
await localesService.delete({ id });
getService('metrics').sendDidUpdateI18nLocalesEvent(ctx.state?.user);
const sanitizedLocale = await sanitizeLocale(existingLocale);
ctx.body = await localesService.setIsDefault(sanitizedLocale);

View File

@ -16,11 +16,17 @@ const count = (params) => strapi.query('plugin::i18n.locale').count({ where: par
const create = async (locale) => {
const result = await strapi.query('plugin::i18n.locale').create({ data: locale });
getService('metrics').sendDidUpdateI18nLocalesEvent();
return result;
};
const update = async (params, updates) => {
const result = await strapi.query('plugin::i18n.locale').update({ where: params, data: updates });
getService('metrics').sendDidUpdateI18nLocalesEvent();
return result;
};
@ -30,6 +36,9 @@ const deleteFn = async ({ id }) => {
if (localeToDelete) {
await deleteAllLocalizedEntriesFor({ locale: localeToDelete.code });
const result = await strapi.query('plugin::i18n.locale').delete({ where: { id } });
getService('metrics').sendDidUpdateI18nLocalesEvent();
return result;
}

View File

@ -11,7 +11,6 @@ const sendDidInitializeEvent = async () => {
0
)(strapi.contentTypes);
// This event is anonymous
await strapi.telemetry.send('didInitializeI18n', { groupProperties: { numberOfContentTypes } });
};