Skip to content
Draft
4 changes: 2 additions & 2 deletions packages/common/actions/entities/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ export const createAnonymousLinkRequest = () => ({
type: CREATE_ANONYMOUS_LINK_REQUEST,
});

export const createAnonymousLinkSuccess = (itemId, share) => ({
export const createAnonymousLinkSuccess = (itemId, shared) => ({
type: CREATE_ANONYMOUS_LINK_SUCCESS,
payload: {
itemId,
share,
shared,
},
});

Expand Down
9 changes: 5 additions & 4 deletions packages/common/actions/entities/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ export const fetchTeamMembersFailure = () => ({
type: FETCH_TEAM_MEMBERS_FAILURE,
});

export const createMemberRequest = (email, role) => ({
// @Deprecated
export const createMemberRequest = (email, teamRole) => ({
type: CREATE_MEMBER_REQUEST,
payload: {
email,
role,
teamRole,
},
});

Expand All @@ -77,11 +78,11 @@ export const createMemberFailure = () => ({
type: CREATE_MEMBER_FAILURE,
});

export const createMemberBatchRequest = (email, role) => ({
export const createMemberBatchRequest = (email, teamRole) => ({
type: CREATE_MEMBER_BATCH_REQUEST,
payload: {
email,
role,
teamRole,
},
});

Expand Down
20 changes: 20 additions & 0 deletions packages/common/actions/entities/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export const FETCH_USERS_REQUEST = '@user/FETCH_USERS_REQUEST';
export const FETCH_USERS_SUCCESS = '@user/FETCH_USERS_SUCCESS';
export const FETCH_USERS_FAILURE = '@user/FETCH_USERS_FAILURE';

export const CREATE_USER_REQUEST = '@user/CREATE_USER_REQUEST';
export const CREATE_USER_SUCCESS = '@user/CREATE_USER_SUCCESS';
export const CREATE_USER_FAILURE = '@user/CREATE_USER_FAILURE';

export const RESET_USER_STATE = '@user/RESET_USER_STATE';

export const fetchUsersRequest = () => ({
Expand All @@ -19,6 +23,22 @@ export const fetchUsersFailure = () => ({
type: FETCH_USERS_FAILURE,
});

export const createUserRequest = payload => ({
type: CREATE_USER_REQUEST,
payload,
});

export const createUserSuccess = user => ({
type: CREATE_USER_SUCCESS,
payload: {
user,
},
});

export const createUserFailure = () => ({
type: CREATE_USER_FAILURE,
});

export const resetUserState = () => ({
type: RESET_USER_STATE,
});
5 changes: 5 additions & 0 deletions packages/common/actions/workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const INIT_TEAM_SETTINGS = '@workflow/INIT_TEAM_SETTINGS';
export const INIT_IMPORT_SETTINGS = '@workflow/INIT_IMPORT_SETTINGS';
export const INIT_CREATE_PAGE = '@workflow/INIT_CREATE_PAGE';
export const INIT_DASHBOARD = '@workflow/INIT_DASHBOARD';
export const INIT_SHARE = '@workflow/INIT_SHARE';
export const FINISH_PROCESSING_KEYPAIRS =
'@workflow/FINISH_PROCESSING_KEYPAIRS';

Expand Down Expand Up @@ -69,6 +70,10 @@ export const initDashboard = () => ({
type: INIT_DASHBOARD,
});

export const initShare = () => ({
type: INIT_SHARE,
});

export const finishProcessingKeyPairs = () => ({
type: FINISH_PROCESSING_KEYPAIRS,
});
Expand Down
5 changes: 2 additions & 3 deletions packages/common/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ export const deleteTeam = teamId => callApi.delete(`/teams/${teamId}`);
export const getTeamMembers = teamId => callApi.get(`/teams/${teamId}/members`);
export const getDefaultTeamMembers = () =>
callApi.get('/teams/default/members');
export const postAddTeamMember = ({ teamId, userId, role, secret }) =>
export const postAddTeamMember = ({ teamId, userId, teamRole, secret }) =>
callApi.post(`/teams/${teamId}/members/${userId}`, {
teamRole: role,
teamRole,
secret,
});
export const updateTeamMember = ({ teamId, userId, teamRole }) =>
Expand Down Expand Up @@ -209,7 +209,6 @@ export const getLastUpdatedUserItems = (

export const postItemShare = ({ itemId, users }) =>
callApi.post(`/items/${itemId}/share`, { users });
export const getCheckShare = id => callApi.get(`/anonymous/share/${id}/check`);

// item batch
export const postCreateItemsBatch = data => callApi.post('/items/batch', data);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/reducers/entities/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export default createReducer(initialState, {
...state.byId,
[payload.itemId]: {
...state.byId[payload.itemId],
shared: payload.share,
shared: payload.shared,
},
},
};
Expand Down
18 changes: 18 additions & 0 deletions packages/common/reducers/entities/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
FETCH_USERS_REQUEST,
FETCH_USERS_SUCCESS,
FETCH_USERS_FAILURE,
CREATE_USER_REQUEST,
CREATE_USER_SUCCESS,
CREATE_USER_FAILURE,
RESET_USER_STATE,
} from '@caesar/common/actions/entities/user';

Expand All @@ -27,6 +30,21 @@ export default createReducer(initialState, {
[FETCH_USERS_FAILURE](state) {
return { ...state, isLoading: false, isError: true };
},
[CREATE_USER_REQUEST](state) {
return state;
},
[CREATE_USER_SUCCESS](state, { payload }) {
return {
...state,
byId: {
...state.byId,
[payload.user.id]: payload.user,
},
};
},
[CREATE_USER_FAILURE](state) {
return state;
},
[RESET_USER_STATE]() {
return initialState;
},
Expand Down
57 changes: 18 additions & 39 deletions packages/common/sagas/common/anonymous.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { put, call, takeLatest, select } from 'redux-saga/effects';
import { workInProgressItemSelector } from '@caesar/common/selectors/workflow';
import {
encryptItem,
generateAnonymousEmail,
} from '@caesar/common/utils/cipherUtils';
import { createMemberSaga } from '@caesar/common/sagas/entities/member';
import { generateAnonymousEmail } from '@caesar/common/utils/item';
import { createUserSaga } from '@caesar/common/sagas/entities/user';
import { DOMAIN_ROLES } from '@caesar/common/constants';
import { generateSharingUrl } from '@caesar/common/utils/sharing';
import { objectToBase64 } from '@caesar/common/utils/base64';
Expand All @@ -19,62 +16,44 @@ import {
import { updateWorkInProgressItem } from '@caesar/common/actions/workflow';
import { updateGlobalNotification } from '@caesar/common/actions/application';
import { getServerErrorMessage } from '@caesar/common/utils/error';
import { shareItemBatchSaga } from './share';

export function* createAnonymousLinkSaga() {
try {
const workInProgressItem = yield select(workInProgressItemSelector);

const email = generateAnonymousEmail();
const email = generateAnonymousEmail(workInProgressItem.id);

const {
id: userId,
name,
password,
masterPassword,
publicKey,
} = yield call(createMemberSaga, {
const createdUser = yield call(createUserSaga, {
payload: {
email,
role: DOMAIN_ROLES.ROLE_ANONYMOUS_USER,
domainRole: DOMAIN_ROLES.ROLE_ANONYMOUS_USER,
},
});

// TODO: Add new flow of sharing with keipair.share
// eslint-disable-next-line no-console
console.warn('Not yet implemented');
const encryptedSecret = yield call(
encryptItem,
workInProgressItem.data,
publicKey,
);
const { plainPassword: password, masterPassword } = createdUser;

const item = {
id: 'itemId',
secret: encryptedSecret,
};
yield call(shareItemBatchSaga, {
payload: {
data: { itemIds: [workInProgressItem.id], members: [createdUser] },
},
});

// TODO: Make shorted link
const link = generateSharingUrl(
item.id,
objectToBase64({
e: email,
p: password,
mp: masterPassword,
m: masterPassword,
}),
);

const share = {
id: item.id,
userId,
email,
name,
link,
publicKey,
isAccepted: false,
domainRoles: [DOMAIN_ROLES.ROLE_ANONYMOUS_USER],
};
// TODO: Rename here and on backend to 'anonymLink' or move to secret
// TODO: Encrypt with user key => maybe in secret
const shared = link;

yield put(createAnonymousLinkSuccess(workInProgressItem.id, share));
// TODO: Add request to update 'shared' item
yield put(createAnonymousLinkSuccess(workInProgressItem.id, shared));
yield put(updateWorkInProgressItem());
} catch (error) {
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion packages/common/sagas/common/invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function* inviteNewMemberBatchSaga({ payload: { members } }) {
objectToBase64({
e: email,
p: password,
mp: masterPassword,
m: masterPassword,
}),
),
}));
Expand Down
11 changes: 7 additions & 4 deletions packages/common/sagas/entities/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ export function* updateItemSaga({ payload: { item } }) {

export function* editItemSaga({
payload: { item },
meta: { setSubmitting, notification },
meta: { setSubmitting = Function.prototype, notification } = {},
}) {
try {
const {
Expand Down Expand Up @@ -780,9 +780,12 @@ export function* editItemSaga({
}

yield put(updateWorkInProgressItem(item.id));
yield call(notification.show, {
text: `The '${item.data.name}' has been updated`,
});

if (notification) {
yield call(notification.show, {
text: `The '${item.data.name}' has been updated`,
});
}
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand Down
10 changes: 5 additions & 5 deletions packages/common/sagas/entities/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const setNewFlag = (members, isNew) =>
const renameUserId = members =>
members.map(({ userId, ...member }) => ({ id: userId, ...member }));

// TODO: replace with create user
export function* createMemberSaga({ payload: { email, role } }) {
try {
const { password, masterPassword, publicKey, privateKey } = yield call(
Expand Down Expand Up @@ -102,6 +103,7 @@ export function* createMemberSaga({ payload: { email, role } }) {
}
}

// TODO: replace with create user batch
export function* createMemberBatchSaga({ payload: { emailRolePairs } }) {
try {
if (!emailRolePairs.length) {
Expand Down Expand Up @@ -176,6 +178,7 @@ export function* createMemberBatchSaga({ payload: { emailRolePairs } }) {
}
}

// TODO: For what stuff do we need it?
export function* getOrCreateMemberBatchSaga({ payload: { emailRolePairs } }) {
try {
const emailRoleObject = emailRolePairs.reduce(
Expand Down Expand Up @@ -239,7 +242,7 @@ export function* getOrCreateMemberBatchSaga({ payload: { emailRolePairs } }) {
}
}

export function* addMemberToTeamListsBatchSaga({ payload: { teamId, users } }) {
export function* addTeamMembersBatchSaga({ payload: { teamId, users } }) {
try {
const keypair = yield select(teamKeyPairSelector, { teamId });
const userIds = users.map(user => user.id);
Expand Down Expand Up @@ -330,10 +333,7 @@ export default function* memberSagas() {
yield takeLatest(CREATE_MEMBER_REQUEST, createMemberSaga);
yield takeLatest(CREATE_MEMBER_BATCH_REQUEST, createMemberBatchSaga);
yield takeLatest(FETCH_TEAM_MEMBERS_REQUEST, fetchTeamMembersSaga);
yield takeLatest(
ADD_TEAM_MEMBERS_BATCH_REQUEST,
addMemberToTeamListsBatchSaga,
);
yield takeLatest(ADD_TEAM_MEMBERS_BATCH_REQUEST, addTeamMembersBatchSaga);
yield takeLatest(UPDATE_TEAM_MEMBER_ROLE_REQUEST, updateTeamMemberRoleSaga);
yield takeLatest(REMOVE_TEAM_MEMBER_REQUEST, removeTeamMemberSaga);
}
4 changes: 2 additions & 2 deletions packages/common/sagas/entities/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
pinTeam,
} from '@caesar/common/api';
import { fetchUsersSaga } from '@caesar/common/sagas/entities/user';
import { addMemberToTeamListsBatchSaga } from '@caesar/common/sagas/entities/member';
import { addTeamMembersBatchSaga } from '@caesar/common/sagas/entities/member';
import {
getServerErrorMessage,
getServerErrors,
Expand Down Expand Up @@ -296,7 +296,7 @@ export function* createTeamSaga({
);

if (adminsToInvite.length > 0 && serverTeam?.id) {
yield call(addMemberToTeamListsBatchSaga, {
yield call(addTeamMembersBatchSaga, {
payload: {
teamId: serverTeam?.id,
users: adminsToInvite,
Expand Down
Loading