Skip to content

Commit a02d462

Browse files
authored
Fix superadmin access PalisadoesFoundation#2307 with user-posted media (PalisadoesFoundation#2318)
* Fix superadmin access issue with user-posted media * Refactor code * add test * fix context
1 parent 83fe756 commit a02d462

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

src/resolvers/Organization/posts.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import { MAXIMUM_FETCH_LIMIT } from "../../constants";
2727
*
2828
* @throws GraphQLError Throws an error if the provided arguments are invalid.
2929
*/
30-
export const posts: OrganizationResolvers["posts"] = async (parent, args) => {
30+
export const posts: OrganizationResolvers["posts"] = async (
31+
parent,
32+
args,
33+
context,
34+
) => {
3135
const parseGraphQLConnectionArgumentsResult =
3236
await parseGraphQLConnectionArguments({
3337
args,
@@ -75,12 +79,21 @@ export const posts: OrganizationResolvers["posts"] = async (parent, args) => {
7579
.countDocuments()
7680
.exec(),
7781
]);
82+
const posts = objectList.map((post: InterfacePost) => ({
83+
...post,
84+
imageUrl: post.imageUrl
85+
? new URL(post.imageUrl, context.apiRootUrl).toString()
86+
: null,
87+
videoUrl: post.videoUrl
88+
? new URL(post.videoUrl, context.apiRootUrl).toString()
89+
: null,
90+
}));
7891
return transformToDefaultGraphQLConnection<
7992
ParsedCursor,
8093
InterfacePost,
8194
InterfacePost
8295
>({
83-
objectList,
96+
objectList: posts,
8497
parsedArgs,
8598
totalCount,
8699
});

tests/resolvers/Organization/posts.spec.ts

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
parseCursor,
1010
posts as postResolver,
1111
} from "../../../src/resolvers/Organization/posts";
12-
import type { PostEdge } from "../../../src/types/generatedGraphQLTypes";
1312
import type { DefaultGraphQLArgumentError } from "../../../src/utilities/graphQLConnection";
1413
import { connect, disconnect } from "../../helpers/db";
1514
import { createTestPost, type TestPostType } from "../../helpers/posts";
@@ -71,12 +70,66 @@ describe("resolvers -> Organization -> post", () => {
7170
creatorId: testUser?._id,
7271
}).countDocuments();
7372

74-
expect((connection?.edges[0] as unknown as PostEdge).cursor).toEqual(
75-
testPost2?._id.toString(),
76-
);
77-
expect((connection?.edges[1] as unknown as PostEdge).cursor).toEqual(
78-
testPost?._id.toString(),
79-
);
73+
const context = { apiRootUrl: "http://example.com" };
74+
75+
const formattedPost2 = {
76+
...testPost2?.toObject(),
77+
imageUrl: testPost2?.imageUrl
78+
? new URL(testPost2.imageUrl, context.apiRootUrl).toString()
79+
: null,
80+
videoUrl: testPost2?.videoUrl
81+
? new URL(testPost2.videoUrl, context.apiRootUrl).toString()
82+
: null,
83+
};
84+
85+
const formattedPost = {
86+
...testPost?.toObject(),
87+
imageUrl: testPost?.imageUrl
88+
? new URL(testPost.imageUrl, context.apiRootUrl).toString()
89+
: null,
90+
videoUrl: testPost?.videoUrl
91+
? new URL(testPost.videoUrl, context.apiRootUrl).toString()
92+
: null,
93+
};
94+
95+
expect(connection).toEqual({
96+
edges: [
97+
{
98+
cursor: formattedPost2._id?.toString(),
99+
node: {
100+
...formattedPost2,
101+
_id: formattedPost2._id?.toString(),
102+
imageUrl: testPost?.imageUrl
103+
? new URL(testPost.imageUrl, context.apiRootUrl).toString()
104+
: null,
105+
videoUrl: formattedPost2?.videoUrl
106+
? new URL(formattedPost2.videoUrl, context.apiRootUrl).toString()
107+
: null,
108+
},
109+
},
110+
{
111+
cursor: formattedPost._id?.toString(),
112+
node: {
113+
...formattedPost,
114+
_id: formattedPost?._id?.toString(),
115+
imageUrl: formattedPost?.imageUrl
116+
? new URL(formattedPost.imageUrl, context.apiRootUrl).toString()
117+
: null,
118+
videoUrl: formattedPost?.videoUrl
119+
? new URL(formattedPost.videoUrl, context.apiRootUrl).toString()
120+
: null,
121+
},
122+
},
123+
],
124+
pageInfo: {
125+
endCursor: testPost?._id.toString(),
126+
hasNextPage: false,
127+
hasPreviousPage: false,
128+
startCursor: testPost2?._id.toString(),
129+
},
130+
totalCount,
131+
});
132+
80133
expect(connection?.pageInfo.endCursor).toEqual(testPost?._id.toString());
81134
expect(connection?.pageInfo.hasNextPage).toBe(false);
82135
expect(connection?.pageInfo.hasPreviousPage).toBe(false);

0 commit comments

Comments
 (0)