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

[IPCT1-509] - Media path #207

Merged
merged 7 commits into from
Mar 16, 2022
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
50 changes: 43 additions & 7 deletions packages/api/src/controllers/community.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { services } from '@impactmarket/core';
import { config, services, database } from '@impactmarket/core';
import { Request, Response } from 'express';

import { RequestWithUser } from '../middlewares/core';
Expand Down Expand Up @@ -239,7 +239,7 @@ class CommunityController {
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

create = (req: Request, res: Response) => {
create = async (req: Request, res: Response) => {
const {
requestByAddress, // the address making the request (will be community manager)
name,
Expand All @@ -256,6 +256,17 @@ class CommunityController {
contractParams,
} = req.body;

const appMedia = await database.models.appMediaContent.findOne({
attributes: ['url'],
where: {
id: coverMediaId,
},
});
const coverMediaPath = appMedia!.url.replace(
`${config.cloudfrontUrl}/`,
''
);

services.ubi.CommunityService.create({
requestByAddress,
name,
Expand All @@ -269,13 +280,13 @@ class CommunityController {
email,
txReceipt,
contractParams,
coverMediaId,
coverMediaPath,
})
.then((community) => standardResponse(res, 201, true, community))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

edit = (req: RequestWithUser, res: Response) => {
edit = async (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 400, false, '', {
error: {
Expand All @@ -286,6 +297,19 @@ class CommunityController {
return;
}
const { name, description, currency, coverMediaId, email } = req.body;

const appMedia = await database.models.appMediaContent.findOne({
attributes: ['url'],
where: {
id: coverMediaId,
},
});

const coverMediaPath = appMedia!.url.replace(
`${config.cloudfrontUrl}/`,
''
);

// verify if the current user is manager in this community
services.ubi.ManagerService.get(req.user.address)
.then(async (manager) => {
Expand All @@ -296,7 +320,7 @@ class CommunityController {
name,
description,
currency,
coverMediaId,
coverMediaPath,
email,
},
req.user?.address
Expand Down Expand Up @@ -392,7 +416,7 @@ class CommunityController {
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

editSubmission = (req: RequestWithUser, res: Response) => {
editSubmission = async (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 400, false, '', {
error: {
Expand All @@ -416,6 +440,18 @@ class CommunityController {
contractParams,
} = req.body;

const appMedia = await database.models.appMediaContent.findOne({
attributes: ['url'],
where: {
id: coverMediaId,
},
});

const coverMediaPath = appMedia!.url.replace(
`${config.cloudfrontUrl}/`,
''
);

services.ubi.CommunityService.editSubmission({
requestByAddress: req.user.address,
name,
Expand All @@ -427,7 +463,7 @@ class CommunityController {
gps,
email,
contractParams,
coverMediaId: parseInt(coverMediaId, 10),
coverMediaPath,
})
.then((r) => standardResponse(res, 201, true, r))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
Expand Down
23 changes: 20 additions & 3 deletions packages/api/src/controllers/story.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { services } from '@impactmarket/core';
import { config, database, services } from '@impactmarket/core';
import { Request, Response } from 'express';

import { RequestWithUser } from '../middlewares/core';
Expand All @@ -23,7 +23,7 @@ class StoryController {
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

add = (req: RequestWithUser, res: Response) => {
add = async (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 401, false, '', {
error: {
Expand All @@ -33,8 +33,25 @@ class StoryController {
});
return;
}
const { communityId, message, storyMediaId } = req.body;

const appMedia = await database.models.appMediaContent.findOne({
attributes: ['url'],
where: {
id: storyMediaId,
},
});
const storyMediaPath = appMedia!.url.replace(
`${config.cloudfrontUrl}/`,
''
);

this.storyService
.add(req.user.address, req.body)
.add(req.user.address, {
communityId,
message,
storyMediaPath,
})
.then((r) => standardResponse(res, 200, true, r))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};
Expand Down
34 changes: 27 additions & 7 deletions packages/api/src/controllers/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils, services } from '@impactmarket/core';
import { utils, services, database, config } from '@impactmarket/core';
import { Request, Response } from 'express';

import { RequestWithUser } from '../middlewares/core';
Expand All @@ -12,7 +12,7 @@ class UserController {
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

public auth = (req: Request, res: Response) => {
public auth = async (req: Request, res: Response) => {
const {
address,
phone,
Expand All @@ -26,6 +26,18 @@ class UserController {
overwrite,
recover,
} = req.body;

const appMedia = await database.models.appMediaContent.findOne({
attributes: ['url'],
where: {
id: avatarMediaId,
},
});
const avatarMediaPath = appMedia!.url.replace(
`${config.cloudfrontUrl}/`,
''
);

services.app.UserService.authenticate(
{
address,
Expand All @@ -35,7 +47,7 @@ class UserController {
username,
year,
children,
avatarMediaId,
avatarMediaPath,
trust: {
phone,
},
Expand Down Expand Up @@ -105,7 +117,7 @@ class UserController {
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};

public updateAvatar = (req: RequestWithUser, res: Response) => {
public updateAvatar = async (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 401, false, '', {
error: {
Expand All @@ -116,7 +128,17 @@ class UserController {
return;
}
const { mediaId } = req.body;
services.app.UserService.updateAvatar(req.user.address, mediaId)
const appMedia = await database.models.appMediaContent.findOne({
attributes: ['url'],
where: {
id: mediaId,
},
});
const avatarMediaPath = appMedia!.url.replace(
`${config.cloudfrontUrl}/`,
''
);
services.app.UserService.updateAvatar(req.user.address, avatarMediaPath)
.then((r) => standardResponse(res, 201, r, ''))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};
Expand Down Expand Up @@ -265,7 +287,6 @@ class UserController {
username,
year,
children,
avatarMediaId,
} = req.body;
services.app.UserService.authenticate({
address,
Expand All @@ -275,7 +296,6 @@ class UserController {
username,
year,
children,
avatarMediaId,
trust: {
phone,
},
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/routes/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export default (app: Router): void => {
* type: string
* required: true
* coverMediaId:
* type: number
* type: integer
* required: true
* txReceipt:
* type: object
Expand Down Expand Up @@ -1005,7 +1005,7 @@ export default (app: Router): void => {
* type: string
* required: false
* coverMediaId:
* type: number
* type: integer
* required: true
* responses:
* "200":
Expand Down Expand Up @@ -1062,7 +1062,7 @@ export default (app: Router): void => {
* type: string
* required: true
* coverMediaId:
* type: number
* type: integer
* required: true
* gps:
* type: object
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default (app: Router): void => {
* type: number
* required: false
* avatarMediaId:
* type: number
* type: integer
* required: false
* responses:
* "200":
Expand Down Expand Up @@ -228,7 +228,7 @@ export default (app: Router): void => {
* type: number
* required: false
* avatarMediaId:
* type: number
* type: integer
* required: false
* responses:
* "200":
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/database/migrations/create-community.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ module.exports = {
},
coverMediaId: {
type: Sequelize.INTEGER,
allowNull: false,
allowNull: true,
},
coverMediaPath: {
type: Sequelize.STRING(44),
allowNull: true,
},
status: {
type: Sequelize.ENUM('pending', 'valid', 'removed'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = {
type: Sequelize.INTEGER,
allowNull: true,
},
avatarMediaPath: {
type: Sequelize.STRING(44),
allowNull: true,
},
username: {
type: Sequelize.STRING(128),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module.exports = {
// onDelete: 'SET NULL', // default
allowNull: true,
},
storyMediaPath: {
type: Sequelize.STRING(44),
allowNull: true,
},
message: {
type: Sequelize.STRING(256),
allowNull: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

// eslint-disable-next-line no-undef
module.exports = {
async up(queryInterface, Sequelize) {
if (process.env.NODE_ENV === 'test') {
return;
}
await queryInterface.addColumn('app_user', 'avatarMediaPath', {
type: Sequelize.STRING(44),
allowNull: true,
});
},

down(queryInterface, Sequelize) {},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

// eslint-disable-next-line no-undef
module.exports = {
async up(queryInterface, Sequelize) {
if (process.env.NODE_ENV === 'test') {
return;
}
await queryInterface.addColumn('story_content', 'storyMediaPath', {
type: Sequelize.STRING(44),
allowNull: true,
});
},

down(queryInterface, Sequelize) {},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

// eslint-disable-next-line no-undef
module.exports = {
async up(queryInterface, Sequelize) {
if (process.env.NODE_ENV === 'test') {
return;
}
await queryInterface.changeColumn('community', 'coverMediaId', {
type: Sequelize.INTEGER,
allowNull: true,
});
await queryInterface.addColumn('community', 'coverMediaPath', {
type: Sequelize.STRING(44),
allowNull: true,
});
},

down(queryInterface, Sequelize) {},
};
5 changes: 5 additions & 0 deletions packages/core/src/database/models/app/appUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class AppUserModel extends Model<AppUser, AppUserCreationAttributes> {
public address!: string;
public username!: string | null;
public avatarMediaId!: number | null;
public avatarMediaPath!: string | null;
public language!: string;
public currency!: string;
public pushNotificationToken!: string | null;
Expand Down Expand Up @@ -49,6 +50,10 @@ export function initializeAppUser(sequelize: Sequelize): typeof AppUserModel {
// onDelete: 'SET NULL', // default
allowNull: true,
},
avatarMediaPath: {
type: DataTypes.STRING(44),
allowNull: true
},
username: {
type: DataTypes.STRING(128),
},
Expand Down
Loading