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

support: Improve typings for PageService #9220

Merged
merged 1 commit into from
Oct 8, 2024
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
5 changes: 3 additions & 2 deletions apps/app/src/server/routes/apiv3/page/update-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isTopPage, isUsersProtectedPages } from '@growi/core/dist/utils/page-pa
import type { Request, RequestHandler } from 'express';
import type { ValidationChain } from 'express-validator';
import { body } from 'express-validator';
import type { HydratedDocument } from 'mongoose';
import mongoose from 'mongoose';

import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity';
Expand Down Expand Up @@ -68,7 +69,7 @@ export const updatePageHandlersFactory: UpdatePageHandlersFactory = (crowi) => {
];


async function postAction(req: UpdatePageRequest, res: ApiV3Response, updatedPage: PageDocument, previousRevision: IRevisionHasId | null) {
async function postAction(req: UpdatePageRequest, res: ApiV3Response, updatedPage: HydratedDocument<PageDocument>, previousRevision: IRevisionHasId | null) {
// Reflect the updates in ydoc
const origin = req.body.origin;
if (origin === Origin.View || origin === undefined) {
Expand Down Expand Up @@ -168,7 +169,7 @@ export const updatePageHandlersFactory: UpdatePageHandlersFactory = (crowi) => {
return res.apiv3Err(new ErrorV3('Posted param "revisionId" is outdated.', PageUpdateErrorCode.CONFLICT, undefined, { returnLatestRevision }), 409);
}

let updatedPage: PageDocument;
let updatedPage: HydratedDocument<PageDocument>;
let previousRevision: IRevisionHasId | null;
try {
const {
Expand Down
34 changes: 16 additions & 18 deletions apps/app/src/server/service/page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,10 @@ class PageCursorsForDescendantsFactory {

private initialCursor: Cursor<any> | never[]; // TODO: wait for mongoose update

private Page: PageModel;

constructor(user: any, rootPage: any, shouldIncludeEmpty: boolean) {
this.user = user;
this.rootPage = rootPage;
this.shouldIncludeEmpty = shouldIncludeEmpty;

this.Page = mongoose.model('Page') as unknown as PageModel;
}

// prepare initial cursor
Expand Down Expand Up @@ -150,9 +146,10 @@ class PageCursorsForDescendantsFactory {
return [];
}

const { PageQueryBuilder } = this.Page;
const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
const { PageQueryBuilder } = Page;

const builder = new PageQueryBuilder(this.Page.find(), this.shouldIncludeEmpty);
const builder = new PageQueryBuilder(Page.find(), this.shouldIncludeEmpty);
builder.addConditionToFilteringByParentId(page._id);

const cursor = builder.query.lean().cursor({ batchSize: BULK_REINDEX_SIZE }) as Cursor<any>;
Expand Down Expand Up @@ -3653,13 +3650,13 @@ class PageService implements IPageService {

// --------- Create ---------

private async preparePageDocumentToCreate(path: string, shouldNew: boolean): Promise<PageDocument> {
const Page = mongoose.model('Page') as unknown as PageModel;
private async preparePageDocumentToCreate(path: string, shouldNew: boolean): Promise<HydratedDocument<PageDocument>> {
const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');

const emptyPage = await Page.findOne({ path, isEmpty: true });

// Use empty page if exists, if not, create a new page
let page;
let page: HydratedDocument<PageDocument>;
if (shouldNew) {
page = new Page();
}
Expand Down Expand Up @@ -3773,7 +3770,7 @@ class PageService implements IPageService {
* Create a page
* Set options.isSynchronously to true to await all process when you want to run this method multiple times at short intervals.
*/
async create(_path: string, body: string, user: HasObjectId, options: IOptionsForCreate = {}): Promise<PageDocument> {
async create(_path: string, body: string, user: HasObjectId, options: IOptionsForCreate = {}): Promise<HydratedDocument<PageDocument>> {
// Switch method
const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
if (!isV5Compatible) {
Expand All @@ -3784,7 +3781,7 @@ class PageService implements IPageService {
const path: string = generalXssFilter.process(_path); // sanitize path

// Retrieve closest ancestor document
const Page = mongoose.model<PageDocument, PageModel>('Page');
const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
const closestAncestor = await Page.findNonEmptyClosestAncestor(path);

// Determine grantData
Expand Down Expand Up @@ -3902,7 +3899,7 @@ class PageService implements IPageService {
* V4 compatible create method
*/
private async createV4(path, body, user, options: any = {}) {
const Page = mongoose.model('Page') as unknown as PageModel;
const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');

const format = options.format || 'markdown';
const grantUserGroupIds = options.grantUserGroupIds || null;
Expand Down Expand Up @@ -4078,7 +4075,7 @@ class PageService implements IPageService {
}

async updatePageSubOperation(page, user, exPage, options: IOptionsForUpdate, pageOpId: ObjectIdLike): Promise<void> {
const Page = mongoose.model('Page') as unknown as PageModel;
const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');

const currentPage = page;

Expand Down Expand Up @@ -4143,13 +4140,13 @@ class PageService implements IPageService {
}

async updatePage(
pageData: PageDocument,
pageData: HydratedDocument<PageDocument>,
body: string | null,
previousBody: string | null,
user: IUserHasId,
options: IOptionsForUpdate = {},
): Promise<PageDocument> {
const Page = mongoose.model('Page') as unknown as PageModel;
): Promise<HydratedDocument<PageDocument>> {
const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');

const wasOnTree = pageData.parent != null || isTopPage(pageData.path);
const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
Expand Down Expand Up @@ -4291,8 +4288,9 @@ class PageService implements IPageService {
}


async updatePageV4(pageData: PageDocument, body, previousBody, user, options: IOptionsForUpdate = {}): Promise<PageDocument> {
const Page = mongoose.model('Page') as unknown as PageModel;
async updatePageV4(
pageData: HydratedDocument<PageDocument>, body, previousBody, user, options: IOptionsForUpdate = {},
): Promise<HydratedDocument<PageDocument>> {

// use the previous data if absent
const grant = options.grant || pageData.grant;
Expand Down
8 changes: 5 additions & 3 deletions apps/app/src/server/service/page/page-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
HasObjectId,
IPageInfo, IPageInfoForEntity, IUser,
} from '@growi/core';
import type { Types } from 'mongoose';
import type { HydratedDocument, Types } from 'mongoose';

import type { IOptionsForCreate, IOptionsForUpdate } from '~/interfaces/page';
import type { PopulatedGrantedGroup } from '~/interfaces/page-grant';
Expand All @@ -13,9 +13,11 @@ import type { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
import type { PageDocument } from '~/server/models/page';

export interface IPageService {
create(path: string, body: string, user: HasObjectId, options: IOptionsForCreate): Promise<PageDocument>,
create(path: string, body: string, user: HasObjectId, options: IOptionsForCreate): Promise<HydratedDocument<PageDocument>>,
forceCreateBySystem(path: string, body: string, options: IOptionsForCreate): Promise<PageDocument>,
updatePage(pageData: PageDocument, body: string | null, previousBody: string | null, user: IUser, options: IOptionsForUpdate,): Promise<PageDocument>,
updatePage(
pageData: HydratedDocument<PageDocument>, body: string | null, previousBody: string | null, user: IUser, options: IOptionsForUpdate
): Promise<HydratedDocument<PageDocument>>,
updateDescendantCountOfAncestors: (pageId: ObjectIdLike, inc: number, shouldIncludeTarget: boolean) => Promise<void>,
deleteCompletelyOperation: (pageIds: ObjectIdLike[], pagePaths: string[]) => Promise<void>,
getEventEmitter: () => EventEmitter,
Expand Down
Loading