chore: optimize data handle logic

This commit is contained in:
fantasticit 2022-05-27 15:46:04 +08:00
parent dadd098800
commit a48f5669d8
4 changed files with 63 additions and 55 deletions

View File

@ -30,7 +30,7 @@ export const useRecentDocuments = () => {
const { data, error, isLoading, refetch } = useQuery( const { data, error, isLoading, refetch } = useQuery(
DocumentApiDefinition.recent.client(), DocumentApiDefinition.recent.client(),
getRecentVisitedDocuments, getRecentVisitedDocuments,
{ staleTime: 0, refetchOnMount: true } { staleTime: 3000 }
); );
return { data, error, loading: isLoading, refresh: refetch }; return { data, error, loading: isLoading, refresh: refetch };
}; };

View File

@ -175,8 +175,10 @@ export const getWikiDetail = (wikiId, cookie = null): Promise<IWiki> => {
* @returns * @returns
*/ */
export const useWikiDetail = (wikiId) => { export const useWikiDetail = (wikiId) => {
const { data, error, isLoading, refetch } = useQuery(WikiApiDefinition.getDetailById.client(wikiId), () => const { data, error, isLoading, refetch } = useQuery(
wikiId ? getWikiDetail(wikiId) : null WikiApiDefinition.getDetailById.client(wikiId),
() => (wikiId ? getWikiDetail(wikiId) : null),
{ staleTime: 3000 }
); );
/** /**

View File

@ -175,13 +175,14 @@ export class DocumentService {
* @returns * @returns
*/ */
async addDocUser(user: OutUser, dto: DocAuthDto) { async addDocUser(user: OutUser, dto: DocAuthDto) {
const doc = await this.documentRepo.findOne(dto.documentId);
const targetUser = await this.userService.findOne({ name: dto.userName }); const targetUser = await this.userService.findOne({ name: dto.userName });
if (!targetUser) { if (!targetUser) {
throw new HttpException('用户不存在', HttpStatus.BAD_REQUEST); throw new HttpException('用户不存在', HttpStatus.BAD_REQUEST);
} }
const doc = await this.documentRepo.findOne(dto.documentId);
await this.wikiService.addWikiUser(user, doc.wikiId, { await this.wikiService.addWikiUser(user, doc.wikiId, {
userName: targetUser.name, userName: targetUser.name,
userRole: WikiUserRole.normal, userRole: WikiUserRole.normal,
@ -250,6 +251,7 @@ export class DocumentService {
*/ */
async getDocUsers(user: OutUser, documentId) { async getDocUsers(user: OutUser, documentId) {
const doc = await this.documentRepo.findOne({ id: documentId }); const doc = await this.documentRepo.findOne({ id: documentId });
if (!doc) { if (!doc) {
throw new HttpException('文档不存在', HttpStatus.BAD_REQUEST); throw new HttpException('文档不存在', HttpStatus.BAD_REQUEST);
} }
@ -411,21 +413,19 @@ export class DocumentService {
* @returns * @returns
*/ */
public async getDocumentDetail(user: OutUser, documentId: string, userAgent) { public async getDocumentDetail(user: OutUser, documentId: string, userAgent) {
// 1. 记录访问 // 异步记录访问
await this.viewService.create({ userId: user.id, documentId, userAgent }); this.viewService.create({ userId: user.id, documentId, userAgent });
// 2. 查询文档 const [document, authority, views] = await Promise.all([
const document = await this.documentRepo.findOne(documentId); this.documentRepo.findOne(documentId),
// 3. 查询权限 this.documentAuthorityRepo.findOne({
const authority = await this.documentAuthorityRepo.findOne({
documentId, documentId,
userId: user.id, userId: user.id,
}); }),
// 4. 查询访问 this.viewService.getDocumentTotalViews(documentId),
const views = await this.viewService.getDocumentTotalViews(documentId); ]);
// 5. 生成响应 const doc = lodash.omit(instanceToPlain(document), ['state', 'content']);
const doc = instanceToPlain(document);
const createUser = await this.userService.findById(doc.createUserId); const createUser = await this.userService.findById(doc.createUserId);
return { document: lodash.omit({ ...doc, views, createUser }, ['state', 'content']), authority }; return { document: { ...doc, views, createUser }, authority };
} }
/** /**
@ -474,12 +474,16 @@ export class DocumentService {
throw new HttpException('密码错误,请重新输入', HttpStatus.BAD_REQUEST); throw new HttpException('密码错误,请重新输入', HttpStatus.BAD_REQUEST);
} }
await this.viewService.create({ userId: 'public', documentId, userAgent }); const doc = lodash.omit(document, ['state']);
const views = await this.viewService.getDocumentTotalViews(documentId); const [views, createUser, wiki] = await Promise.all([
const createUser = await this.userService.findById(document.createUserId); this.viewService.getDocumentTotalViews(documentId),
const wiki = await this.wikiService.getPublicWikiDetail(document.wikiId); this.userService.findById(document.createUserId),
this.wikiService.getPublicWikiDetail(document.wikiId),
]);
// 异步创建
this.viewService.create({ userId: 'public', documentId, userAgent });
return { ...document, views, wiki, createUser }; return { ...doc, views, wiki, createUser };
} }
/** /**
@ -536,14 +540,14 @@ export class DocumentService {
return lodash.omit(item, ['content', 'state']); return lodash.omit(item, ['content', 'state']);
}); });
const docsWithCreateUser = await Promise.all( // const docsWithCreateUser = await Promise.all(
docs.map(async (doc) => { // docs.map(async (doc) => {
const createUser = await this.userService.findById(doc.createUserId); // const createUser = await this.userService.findById(doc.createUserId);
return { ...doc, createUser }; // return { ...doc, createUser };
}) // })
); // );
return docsWithCreateUser; return docs;
} }
async getShareChildrenDocuments(data: { wikiId: string; documentId?: string }) { async getShareChildrenDocuments(data: { wikiId: string; documentId?: string }) {
@ -590,14 +594,14 @@ export class DocumentService {
return lodash.omit(item, ['content', 'state']); return lodash.omit(item, ['content', 'state']);
}); });
const docsWithCreateUser = await Promise.all( // const docsWithCreateUser = await Promise.all(
docs.map(async (doc) => { // docs.map(async (doc) => {
const createUser = await this.userService.findById(doc.createUserId); // const createUser = await this.userService.findById(doc.createUserId);
return { ...doc, createUser }; // return { ...doc, createUser };
}) // })
); // );
return docsWithCreateUser; return docs;
} }
/** /**
@ -627,8 +631,10 @@ export class DocumentService {
await this.userService.findById(doc.createUserId), await this.userService.findById(doc.createUserId),
]); ]);
const optimizedDoc = lodash.omit(doc, ['state', 'content', 'index', 'createUserId']);
return { return {
...instanceToPlain(doc), ...optimizedDoc,
views, views,
visitedAt: visitedAtMap[documentId], visitedAt: visitedAtMap[documentId],
createUser, createUser,
@ -636,9 +642,7 @@ export class DocumentService {
}) })
); );
return ret.filter(Boolean).map((item) => { return ret.filter(Boolean);
return lodash.omit(item, ['state', 'content', 'index', 'createUserId']);
});
} }
/** /**
@ -664,13 +668,13 @@ export class DocumentService {
const data = ret.filter(Boolean); const data = ret.filter(Boolean);
const withCreateUserRes = await Promise.all( // const withCreateUserRes = await Promise.all(
data.map(async (doc) => { // data.map(async (doc) => {
const createUser = await this.userService.findById(doc.createUserId); // const createUser = await this.userService.findById(doc.createUserId);
return { createUser, ...doc }; // return { createUser, ...doc };
}) // })
); // );
return withCreateUserRes; return data;
} }
} }

View File

@ -559,14 +559,14 @@ export class WikiService {
return lodash.omit(item, ['content', 'state']); return lodash.omit(item, ['content', 'state']);
}); });
const docsWithCreateUser = await Promise.all( // const docsWithCreateUser = await Promise.all(
docs.map(async (doc) => { // docs.map(async (doc) => {
const createUser = await this.userService.findById(doc.createUserId); // const createUser = await this.userService.findById(doc.createUserId);
return { ...doc, createUser }; // return { ...doc, createUser };
}) // })
); // );
return array2tree(docsWithCreateUser); return array2tree(docs);
} }
/** /**
@ -576,6 +576,8 @@ export class WikiService {
* @param relations * @param relations
*/ */
public async orderWikiTocs(relations: Array<{ id: string; parentDocumentId?: string; index: number }>) { public async orderWikiTocs(relations: Array<{ id: string; parentDocumentId?: string; index: number }>) {
if (!relations.length) return;
await Promise.all( await Promise.all(
relations.map(async (relation) => { relations.map(async (relation) => {
const { id, parentDocumentId, index } = relation; const { id, parentDocumentId, index } = relation;