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

Avoid delete syntax when pack models #4078

Closed
acid-chicken opened this issue Feb 1, 2019 · 6 comments
Closed

Avoid delete syntax when pack models #4078

acid-chicken opened this issue Feb 1, 2019 · 6 comments
Assignees
Labels
packages/backend Server side specific issue/PR 💚Refactor Rewriting code without changing behavior 🔒Security Security related issue/PR

Comments

@acid-chicken
Copy link
Member

acid-chicken commented Feb 1, 2019

Summary

#4045 のような消し忘れが危ない。

@acid-chicken acid-chicken added packages/backend Server side specific issue/PR 💚Refactor Rewriting code without changing behavior 🔒Security Security related issue/PR labels Feb 1, 2019
@acid-chicken acid-chicken self-assigned this Feb 1, 2019
@mei23
Copy link
Contributor

mei23 commented Feb 1, 2019

Related #4018

@acid-chicken
Copy link
Member Author

例えばこんな感じ。

import * as mongo from 'mongodb';
import * as deepcopy from 'deepcopy';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packUser } from './user';

const AbuseUserReport = db.get<IAbuseUserReport>('abuseUserReports');
AbuseUserReport.createIndex('userId');
AbuseUserReport.createIndex('reporterId');
AbuseUserReport.createIndex(['userId', 'reporterId'], { unique: true });
export default AbuseUserReport;

export interface IAbuseUserReport {
	_id: mongo.ObjectID;
	createdAt: Date;
	userId: mongo.ObjectID;
	reporterId: mongo.ObjectID;
	comment: string;
}

type ReportType = string | mongo.ObjectID | IAbuseUserReport;

export const find = async (report: ReportType) =>
	isObjectId(report) ? await AbuseUserReport.findOne({ _id: report }) :
	typeof report === 'string' && mongo.ObjectID.isValid(report) ? await AbuseUserReport.findOne({ _id: new mongo.ObjectID(report) }) :
	report as IAbuseUserReport;

export const packMany = (reports: ReportType[]) =>
	Promise.all(reports.map(pack));

export const pack = async (report: ReportType) => {
	const source = await find(report);

	return {
		id: source._id,
		reporter: await packUser(source.reporterId, null, { detail: true }),
		user: await packUser(source.userId, null, { detail: true })
	};
};

@mei23
Copy link
Contributor

mei23 commented Feb 1, 2019

(どこら辺が例えばなのかわからない…)
id: source._id, みたいに個々の必要な値ごとに設定するという意味ならそんな感じでいいとは思います

@mei23
Copy link
Contributor

mei23 commented Feb 1, 2019

直接関係ないですが
import { pack as packUser } from './user';みたいに別名でインポートしたくないので

最初から

export const packUser 
export const packAbuseUserReport
``
みたいに定義したい気がする

@acid-chicken
Copy link
Member Author

id: source._id, みたいに個々の必要な値ごとに設定するという意味ならそんな感じでいいとは思います

その通りです。

@acid-chicken
Copy link
Member Author

実質 Duplicate of #4018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
packages/backend Server side specific issue/PR 💚Refactor Rewriting code without changing behavior 🔒Security Security related issue/PR
Projects
None yet
Development

No branches or pull requests

2 participants