Skip to content

Commit

Permalink
Merge pull request #207 from boostcamp-2020/develop
Browse files Browse the repository at this point in the history
4주차 배포 Hotfix
  • Loading branch information
pkiop authored Dec 11, 2020
2 parents 1786eed + d3ead0a commit 1176651
Show file tree
Hide file tree
Showing 106 changed files with 2,274 additions and 214 deletions.
36 changes: 34 additions & 2 deletions be/src/controllers/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,38 @@ export const get = async (ctx: Koa.Context) => {

if (!res) {
ctx.status = 204;
ctx.response.body = [];
return;
}
ctx.status = 200;
ctx.response.body = res;
};

export const postAccount = async (ctx: Koa.Context) => {
await accountService.addAccountByUserAndAccountInfo(
ctx.request.body.user,
ctx.request.body.title,
ctx.request.body.userObjIdList,
);
ctx.status = 201;
ctx.response.body = { success: true };
};

export const putAccount = async (ctx: Koa.Context) => {
await accountService.updateAccountByUserAndAccountInfo(
ctx.request.body.title,
ctx.params.accountObjId,
ctx.request.body.userObjIdList,
);
ctx.status = 200;
ctx.response.body = { success: true };
};

export const deleteAccount = async (ctx: Koa.Context) => {
await accountService.deleteAccountByAccountInfo(ctx.params.accountObjId);
ctx.status = 200;
ctx.response.body = { success: true };
};

export const getThisAccountInfo = async (ctx: Koa.Context) => {
const { title, owner } = ctx.query;
const account = await accountService.getAccountByTitleAndOwner(title, owner);
Expand All @@ -31,4 +56,11 @@ export const postAccountUser = async (ctx: Koa.Context) => {
ctx.body = { message: 'success' };
};

export default { get, getThisAccountInfo, postAccountUser };
export default {
get,
postAccount,
putAccount,
deleteAccount,
getThisAccountInfo,
postAccountUser,
};
15 changes: 15 additions & 0 deletions be/src/controllers/mms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Context } from 'koa';
import { postMms } from 'services/mms';

const post = async (ctx: Context) => {
const { accountObjId, mmsObj, client } = ctx.request.body;
try {
const newMms = await postMms(accountObjId, mmsObj, client);
ctx.status = 200;
ctx.body = newMms;
} catch (e) {
ctx.body = { success: false, message: e };
}
};

export default { post };
26 changes: 24 additions & 2 deletions be/src/controllers/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import Koa from 'koa';
import { Context } from 'koa';
import * as userService from 'services/user';

export const titleByAccountId = async (ctx: Koa.Context) => {
export const titleByAccountId = async (ctx: Context) => {
const { accountId } = ctx.query;
const title = await userService.titleByAccountId(accountId);
ctx.status = 200;
ctx.response.body = title;
};

export const getUserList = async (ctx: Context) => {
const userList = await userService.getUserList();
ctx.status = 200;
ctx.body = userList;
};

export const getUserByAccessToken = async (ctx: Context) => {
ctx.status = 200;
ctx.body = ctx.request.body.user;
};
export const getInvitation = async (ctx: Context) => {
const { user } = ctx.request.body;
const accounts = await userService.getInvitation(user);
const invitations = accounts.map((account: any, idx) => ({
accountObjId: account._id,
title: account.title,
ownerName: account.ownerName,
host: user.invitations[idx].host,
}));
ctx.body = invitations;
};

export default titleByAccountId;
20 changes: 12 additions & 8 deletions be/src/libs/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export const invalidAccessError = {
message: '해당 가계부에 접근 권한이 없습니다',
};

export const updateUnclassifiedMethod = {
status: 200,
error: '수정할 수 없는 항목 입니다',
success: false,
};

export const UserHasNoAccount = {
status: 400,
message: 'user가 가지고 있는 account가 없습니다.',
};
export const invalidCategory = {
status: 403,
message: '해당 카테고리에 접근 할 수 없습니다',
Expand All @@ -29,18 +39,12 @@ export const removeUnclassifiedMethod = {
success: false,
};

export const updateUnclassifiedMethod = {
status: 200,
error: '수정할 수 없는 항목 입니다',
success: false,
};

export const invaildMethod = {
status: 400,
message: '잘못된 자원으로 접근하였습니다',
};

export const UserHasNoAccount = {
export const accountNoChange = {
status: 400,
message: 'user가 가지고 있는 account가 없습니다.',
message: 'account Update 중 에러가 발생했습니다.',
};
12 changes: 11 additions & 1 deletion be/src/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
invalidAccessError,
invalidCategory,
accountHasNoUserError,
updateUnclassifiedMethod,
} from 'libs/error';
import { UserModel } from 'models/user';
import { CategoryModel, categoryType } from 'models/category';

import { AccountModel } from 'models/account';

interface IDecodedData {
Expand Down Expand Up @@ -77,3 +77,13 @@ export const isUnclassifide = async (
}
await next();
};

export const titleIsUnclassified = async (
ctx: Koa.Context,
next: () => Promise<any>,
) => {
const { title } = ctx.request.body;
if (!title || title.trim() === '' || title.trim() === '미분류')
throw updateUnclassifiedMethod;
await next();
};
7 changes: 5 additions & 2 deletions be/src/models/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IAccount {
methods?: string[];
ownerName?: string;
users: Types.DocumentArray<IUserDocument>;
imageUrl?: String;
}

export interface AccountDocument extends Document {
Expand All @@ -29,6 +30,7 @@ export interface AccountDocument extends Document {
methods?: [String];
ownerName?: string;
users: Types.DocumentArray<IUserDocument>;
imageUrl?: String;
}

export interface IAccountDocument extends IAccount, Document {}
Expand Down Expand Up @@ -79,17 +81,18 @@ export const AccountSchema = new Schema({
],
ownerName: String,
users: [UserSchema],
imageUrl: String,
});

AccountSchema.statics.findByPkAndPushTransaction = findByPkAndPushTransaction;
AccountSchema.statics.findByPkAndGetTransCategory = findByPkAndGetTransCategory;
AccountSchema.statics.findByTitleAndOwner = findByTitleAndOwner;
AccountSchema.statics.findAllTransactionExceptDeleted = findAllTransactionExceptDeleted;
AccountSchema.statics.findAccountByUserId = findAccountByUserId;
AccountSchema.statics.findByPkAndPushUser = findByPkAndPushUser;
AccountSchema.statics.findUnclassifiedCategory = findUnclassifiedCategory;
AccountSchema.statics.findUnclassifiedMethod = findUnclassifiedMethod;

AccountSchema.statics.findAccountByUserId = findAccountByUserId;
AccountSchema.statics.findByPkAndPushUser = findByPkAndPushUser;
export const AccountModel = model<IAccountDocument, IAccountModel>(
'accounts',
AccountSchema,
Expand Down
24 changes: 12 additions & 12 deletions be/src/models/account/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,34 @@ export async function findByTitleAndOwner(
) {
if (!title || !owner) throw new NotVaildException();

return this.findOne({ title, owner }, { _id: true }).exec();
return this.findOne({ title, ownerName: owner }, { _id: true }).exec();
}

export async function findUnclassifiedCategory(
export async function findUnclassifiedMethod(
this: IAccountModel,
accountObjId: string,
) {
const res: any = await this.findById(accountObjId, { categories: true })
const res: any = await this.findById(accountObjId, { methods: true })
.populate({
path: 'categories',
match: { type: categoryType.UNCLASSIFIED },
path: 'methods',
match: { title: '미분류' },
select: '_id',
})
.exec();

return res.categories[0]._id;
return res.methods[0]._id;
}

export async function findUnclassifiedMethod(
export async function findUnclassifiedCategory(
this: IAccountModel,
accountObjId: string,
) {
const res: any = await this.findById(accountObjId, { methods: true })
const res: any = await this.findById(accountObjId, { categories: true })
.populate({
path: 'methods',
match: { title: '미분류' },
path: 'categories',
match: { type: categoryType.UNCLASSIFIED },
select: '_id',
})
.exec();
return res.methods[0]._id;

return res.categories[0]._id;
}
22 changes: 15 additions & 7 deletions be/src/models/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Document } from 'mongoose';
import { Schema, model, Document, Types } from 'mongoose';

export const UserSchema = new Schema({
timezone: {
Expand All @@ -16,17 +16,24 @@ export const UserSchema = new Schema({
id: {
type: String,
},
password: {
type: String,
},
salt: {
type: String,
},
profileUrl: {
type: String,
},
invitations: [
{
accounts: {
type: Types.ObjectId,
ref: 'accounts',
},
host: String,
},
],
});

export interface IInvitation {
host: string;
accounts: string;
}
export interface IUserDocument extends Document {
timezone?: String;
startOfWeek?: String;
Expand All @@ -35,6 +42,7 @@ export interface IUserDocument extends Document {
salt?: String;
nickname: String;
profileUrl?: String;
invitations?: Array<IInvitation>;
}

export const UserModel = model<IUserDocument>('users', UserSchema);
3 changes: 3 additions & 0 deletions be/src/routers/api/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const router = new Router();

router.get('/info', accountController.getThisAccountInfo);
router.get('/', accountController.get);
router.post('/', accountController.postAccount);
router.put('/', accountController.putAccount);
router.del('/', accountController.deleteAccount);
router.post('/user', accountController.postAccountUser);

export default router;
6 changes: 3 additions & 3 deletions be/src/routers/api/category/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Router from 'koa-router';
import koaCompose from 'koa-compose';
import categoryController from 'controllers/category';
import { isUnclassifide } from 'middlewares';
import { isUnclassifide, titleIsUnclassified } from 'middlewares';

const router = new Router();

router.get('/statistics', categoryController.getStatisticsInfo);

router.get('/', categoryController.get);
router.post('/', categoryController.post);
router.put('/', categoryController.put);
router.post('/', koaCompose([titleIsUnclassified, categoryController.post]));
router.put('/', koaCompose([titleIsUnclassified, categoryController.put]));
router.delete(
'/:category',
koaCompose([isUnclassifide, categoryController.deleteCategory]),
Expand Down
4 changes: 3 additions & 1 deletion be/src/routers/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import accountRouter from 'routers/api/account';
import { authorization, verifyAccountAccess } from 'middlewares';
import methodRouter from './method';
import categoryRouter from './category';
import mmsRouter from './mms';

const router = new Router();
router.use('/auth', authRouter.routes());

router.use(authorization);
router.use('/user', userRouter.routes());
router.use('/users', userRouter.routes());
router.use('/accounts', accountRouter.routes());
router.use('/mms', mmsRouter.routes());

router.use('/:accountObjId', verifyAccountAccess);
router.use('/:accountObjId/accounts', accountRouter.routes());
Expand Down
9 changes: 7 additions & 2 deletions be/src/routers/api/method/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import Router from 'koa-router';
import methodController from 'controllers/method';
import koaCompose from 'koa-compose';
import { titleIsUnclassified } from 'middlewares';

const router = new Router();

router.get('/', methodController.get);
router.post('/', methodController.post);
router.post('/', koaCompose([titleIsUnclassified, methodController.post]));
router.delete('/:methodObjId', methodController.del);
router.put('/:methodObjId', methodController.put);
router.put(
'/:methodObjId',
koaCompose([titleIsUnclassified, methodController.put]),
);

export default router;
8 changes: 8 additions & 0 deletions be/src/routers/api/mms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Router from 'koa-router';
import mmsController from 'controllers/mms';

const router = new Router();

router.post('/', mmsController.post);

export default router;
7 changes: 7 additions & 0 deletions be/src/routers/api/user/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Router from 'koa-router';
import { getInvitation } from 'controllers/user';

const router = new Router();

router.get('/', getInvitation);
export default router;
4 changes: 4 additions & 0 deletions be/src/routers/api/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Router from 'koa-router';
import * as userController from 'controllers/user';
import accountRouter from './account';

const router = new Router();

router.get('/userInfo', userController.getUserByAccessToken);
router.get('/titleByAccountId', userController.titleByAccountId);
router.get('/', userController.getUserList);
router.use('/accounts', accountRouter.routes());

export default router;
Loading

0 comments on commit 1176651

Please sign in to comment.