Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invitation fix #325

Merged
merged 5 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/api/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ export const signup = catchAsync(async (req, res): Promise<any> => {

// delete the invited account
await userService.deleteUserById(foundInvitedUser.user_id);

// verify the user email
await userService.updateUserById(foundExistingUser.user_id, {
email_verified: true,
});
} else {
// make the user as non-invited and login
user = await userService.updateUserById(userId, {
user_name: foundInvitedUser.user_name || 'anonymous',
is_invited: false,
wallet_address: hashedWalletAddress,
email_verified: true,
});
}

Expand Down
35 changes: 34 additions & 1 deletion app/api/src/controllers/creation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import * as litigationService from '../services/litigation.service';
import { getMaterialById, updateMaterialById } from '../services/material.service';
import { createRecognition } from '../services/recognition.service';
import { getTagById } from '../services/tag.service';
import { IUserDoc } from '../services/user.service';
import { getUserByCriteria, IUserDoc } from '../services/user.service';
import ApiError from '../utils/ApiError';
import catchAsync from '../utils/catchAsync';
import { sendMail } from '../utils/email';
import { generateProofOfCreation } from '../utils/generateProofOfCreation';
import { getSupportedFileTypeFromLink } from '../utils/getSupportedFileTypeFromLink';
import { pinJSON, unpinData } from '../utils/ipfs';
import { encode } from '../utils/jwt';

export const queryCreations = catchAsync(async (req, res): Promise<void> => {
// when req user is requesting their own creations (search_fields has author_id, and query matches auth user id)
Expand Down Expand Up @@ -103,8 +105,24 @@ export const createCreation = catchAsync(async (req, res): Promise<void> => {
// get all materials
// eslint-disable-next-line @typescript-eslint/return-await
const materials = await Promise.all(req.body.materials.map(async (id: string) => await getMaterialById(id)));

await Promise.all(
materials.map(async (m: any) => {
const foundAuthor = await getUserByCriteria('user_id', m.author_id, true);

// send invitation emails to new authors
if (foundAuthor && foundAuthor.is_invited && foundAuthor.email_address) {
await sendMail({
to: foundAuthor?.email_address as string,
subject: `Invitation to recognize authorship of "${m.material_title}"`,
message: `You were recognized as author of "${m.material_title}" by ${
(req.user as IUserDoc)?.user_name
}. Please signup on ${config.web_client_base_url}/signup?token=${encode(
foundAuthor.user_id
)} to be recognized as the author.`,
}).catch(() => null);
}

// send recognition
const recognition = await createRecognition({
recognition_for: m.author_id,
Expand Down Expand Up @@ -205,6 +223,21 @@ export const updateCreationById = catchAsync(async (req, res): Promise<void> =>

await Promise.all(
materials.map(async (m: any) => {
const foundAuthor = await getUserByCriteria('user_id', m.author_id, true);

// send invitation emails to new authors
if (foundAuthor && foundAuthor.is_invited && foundAuthor.email_address) {
await sendMail({
to: foundAuthor?.email_address as string,
subject: `Invitation to recognize authorship of "${m.material_title}"`,
message: `You were recognized as author of "${m.material_title}" by ${
(req.user as IUserDoc)?.user_name
}. Please signup on ${config.web_client_base_url}/signup?token=${encode(
foundAuthor.user_id
)} to be recognized as the author.`,
}).catch(() => null);
}

// send recognition
const recognition = await createRecognition({
recognition_for: m.author_id,
Expand Down
2 changes: 1 addition & 1 deletion app/api/src/controllers/litigation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const createLitigation = catchAsync(async (req, res): Promise<void | any>
}

// check if material can be claimed
if (material && !material?.is_claimable) {
if ((material && !material?.is_claimable) || isCAWPassed || (creation.is_draft && material)) {
throw new ApiError(httpStatus.NOT_FOUND, 'material is not claimable');
}

Expand Down
19 changes: 1 addition & 18 deletions app/api/src/controllers/material.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import config from '../config/config';
import * as materialService from '../services/material.service';
import { getRecognitionById } from '../services/recognition.service';
import { getUserByCriteria, IUserDoc } from '../services/user.service';
import catchAsync from '../utils/catchAsync';
import { sendMail } from '../utils/email';
import { getSupportedFileTypeFromLink } from '../utils/getSupportedFileTypeFromLink';
import { encode } from '../utils/jwt';

export const queryMaterials = catchAsync(async (req, res): Promise<void> => {
const creation = await materialService.queryMaterials(req.query as any);
Expand All @@ -21,10 +18,9 @@ export const getMaterialById = catchAsync(async (req, res): Promise<void> => {

export const createMaterial = catchAsync(async (req, res): Promise<void> => {
// check if reference docs exist
let foundUser = null;
if (req.body.recognition_id) await getRecognitionById(req.body.recognition_id as string); // verify recognition, will throw an error if recognition not found
if (req.body.author_id && req.body.author_id !== (req.user as IUserDoc).user_id) {
foundUser = await getUserByCriteria('user_id', req.body.author_id as string, true); // verify author id (if present), will throw an error if not found
await getUserByCriteria('user_id', req.body.author_id as string, true); // verify author id (if present), will throw an error if not found
}

// get the material type from link
Expand All @@ -37,19 +33,6 @@ export const createMaterial = catchAsync(async (req, res): Promise<void> => {
author_id: req.body.author_id || (req.user as IUserDoc).user_id,
});

// send email to the invited user if found
if (foundUser && foundUser.is_invited && foundUser.email_address) {
await sendMail({
to: foundUser.email_address as string,
subject: `Invitation to recognize authorship of "${newMaterial.material_title}"`,
message: `You were recognized as author of "${newMaterial.material_title}" by ${
(req.user as IUserDoc)?.user_name
}. Please signup on ${config.web_client_base_url}/signup?token=${encode(
foundUser.user_id
)} to be recognized as the author.`,
}).catch(() => null);
}

res.send(newMaterial);
});

Expand Down
44 changes: 33 additions & 11 deletions app/web-frontend/src/pages/Signup/index.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
import LoginForm from 'components/LoginForm';
import useAppKeys from 'hooks/useAppKeys';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import authUser from 'utils/helpers/authUser';

const handleLogout = () => {
authUser.removeJWTToken();
authUser.removeUser();
};

let isAppUpdated = false;

function Signup() {
const navigate = useNavigate();
const { updateAppKey } = useAppKeys();

// get token from url
const parameters = new URLSearchParams(window.location.search);
const token = parameters.get('token');

const handleLogin = () => {
navigate('/wallet');
updateAppKey();
};

useEffect(() => {
handleLogout();
if (!isAppUpdated) { updateAppKey(); isAppUpdated = true; }
}, []);

return (
<div style={{
minHeight: '60vh',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'column',
gap: '16px',
maxWidth: '600px',
margin: 'auto',
}}
<div
style={{
minHeight: '60vh',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'column',
gap: '16px',
maxWidth: '600px',
margin: 'auto',
}}
>
<h4 style={{ fontSize: '28px', fontWeight: 'bold' }}>
Signup with your cardano wallet.
Expand All @@ -28,7 +50,7 @@ function Signup() {
wallet address and you select the same wallet address for signup here we will
merge your invited account into your existing account.
</p>
<LoginForm inviteToken={token} buttonLabel="Signup" onLoggedIn={() => navigate('/wallet')} />
<LoginForm inviteToken={token} buttonLabel="Signup" onLoggedIn={handleLogin} />
</div>
);
}
Expand Down