Skip to content

Commit

Permalink
refactor: rebuild displayComment endpoit
Browse files Browse the repository at this point in the history
  • Loading branch information
mkmak2 committed Jul 17, 2023
1 parent 0be2b6d commit cf3d830
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ const LatestComments = ({ config }) => {
},
};

console.log(result)

const emptyContent = _q ? 'search' : 'comments';

const unreadedComments = result ? result.filter(entity => entity.displayedBy.length === 0).length : null;

const subtitle = <Typography variant='omega' fontSize='20px'>You have {unreadedComments} unreaded comments</Typography>;

return canAccess ? (
<Layout>
Expand All @@ -117,6 +121,8 @@ const LatestComments = ({ config }) => {
<PanelLayout>
<StyledHeader>
<Typography variant='beta' fontSize='30px'>Latest Comments</Typography>
<br/>
{subtitle}
</StyledHeader>
{!isEmpty(result) ? (
<Table colCount={COL_COUNT} rowCount={result.length}>
Expand Down
2 changes: 1 addition & 1 deletion content-types/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default {
},
displayedBy: {
type: "relation",
relation: "oneToMany",
relation: "manyToMany",
target: "plugin::users-permissions.user",
configurable: false
}
Expand Down
38 changes: 20 additions & 18 deletions server/controllers/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ const controllers: IControllerAdmin = {
): ThrowablePromisedResponse<Comment> {

const { params = {}, request } = ctx;
const { body }: ToBeFixed = request;
const { body }: StrapiAdminUser = request;
const { id } = parseParams(params);

try {
Expand All @@ -289,23 +289,6 @@ const controllers: IControllerAdmin = {
}
},

async displayComment(
ctx: StrapiRequestContext<never>
): ThrowablePromisedResponse<Comment> {

const { params = {}, request } = ctx;
const { body }: StrapiAdminUser = request;
const { id } = parseParams(params);

try {
return await this.getService<IServiceAdmin>().displayComment(
id,
body.displayedBy,
);
} catch (e) {
throw throwError(ctx, e);
}
},

async approveComment(
ctx: StrapiRequestContext<never>,
Expand All @@ -328,6 +311,25 @@ const controllers: IControllerAdmin = {
throw throwError(ctx, e);
}
},


async displayComment(
ctx: StrapiRequestContext<never>
): ThrowablePromisedResponse<Comment> {

const { params = {}, request } = ctx;
const { body }:ToBeFixed = request;
const { id } = parseParams(params);

try {
return await this.getService<IServiceAdmin>().displayComment(
id,
body
);
} catch (e) {
throw throwError(ctx, e);
}
},
};

export default controllers;
63 changes: 57 additions & 6 deletions server/services/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
getModelUid,
getRelatedGroups,
filterOurResolvedReports,
getAuthorName,
getAuthorName
} from "./utils/functions";
import { APPROVAL_STATUS, REGEX } from "./../utils/constants";

Expand Down Expand Up @@ -189,6 +189,7 @@ export = ({ strapi }: StrapiContext): IServiceAdmin => ({
resolved: false,
},
},
displayedBy: true,
},
});
const total = await strapi.db.query<Comment>(getModelUid("comment")).count({
Expand Down Expand Up @@ -738,17 +739,67 @@ export = ({ strapi }: StrapiContext): IServiceAdmin => ({
async displayComment(
this: IServiceAdmin,
id: Id,
body: ToBeFixed,
body: string,
): Promise<Comment> {

const defaultAuthorUserPopulate = this.getDefaultAuthorPopulate();

const reportsPopulation = {
reports: {
where: {
resolved: false,
},
},
};

const defaultPopulate = {
populate: {
authorUser: defaultAuthorUserPopulate,
threadOf: {
populate: {
authorUser: defaultAuthorUserPopulate,
...reportsPopulation,
},
},
...reportsPopulation,
displayedBy: true
},
};

const entity = await strapi.db
.query<Comment>(getModelUid("comment"))
.findOne({
where: { id },
...defaultPopulate,
});

if (!entity) {
throw new PluginError(404, "Not found");
}

const user: StrapiAdminUser = await strapi.plugins['users-permissions'].services.user.fetch({ id: body });

if (!user) {
throw new PluginError(404, "Not found");
}

const isUserExistingInRelation = entity.displayedBy?.findIndex( user => user.id === body);

if ( isUserExistingInRelation !== -1) {
throw new PluginError(403, "User is already existing in this relation field");
}

const updatedDisplayedBy = entity.displayedBy?.push(user);

const displayComment = await strapi.db
.query<Comment>(getModelUid("comment"))
.update({
where: { id },
data: {
displayedBy: body
}
data: { displayedBy: updatedDisplayedBy}

});
return this.getCommonService().sanitizeCommentEntity(displayComment);

return this.getCommonService().sanitizeCommentEntity(displayComment);
},

// Recognize Strapi User fields possible to populate
Expand Down
2 changes: 1 addition & 1 deletion types/services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export interface IServiceAdmin {
| undefined;
postComment(threadId: Id, body: string, author: StrapiAdminUser): Promise<Comment>;
updateComment(id: Id, body: string): Promise<Comment>;
displayComment(id: Id, body: StrapiAdminUser): Promise<Comment>;
displayComment(id: Id, body: Id): Promise<Comment>;
}

export interface IServiceClient {
Expand Down

0 comments on commit cf3d830

Please sign in to comment.