Skip to content

Commit

Permalink
fix: invalid url error while fetching advertisements (PalisadoesFound…
Browse files Browse the repository at this point in the history
…ation#2226)

* fix: invalid url error while fetching advertisements

* fix: failing tests

* fix: failing tests

* fix: formatting issues

* improved code coverage
  • Loading branch information
disha1202 authored Apr 27, 2024
1 parent 0730681 commit a7fa6e3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 10 deletions.
3 changes: 1 addition & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ enum AdvertisementType {

type AdvertisementsConnection {
edges: [AdvertisementEdge]
pageInfo: ConnectionPageInfo
pageInfo: DefaultConnectionPageInfo!
totalCount: Int
}

Expand Down Expand Up @@ -635,7 +635,6 @@ type Event {
recurring: Boolean!
startDate: Date!
startTime: Time
status: Status!
title: String!
updatedAt: DateTime!
}
Expand Down
9 changes: 5 additions & 4 deletions src/resolvers/Mutation/updateAdvertisement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,16 @@ export const updateAdvertisement: MutationResolvers["updateAdvertisement"] =
}
}

const fieldsToUpdate = args.input.mediaFile
? { ...args.input, mediaUrl: uploadMediaFile }
: { ...args.input };

const updatedAdvertisement = await Advertisement.findOneAndUpdate(
{
_id: _id,
},
{
$set: {
...args.input,
mediaUrl: uploadMediaFile,
},
$set: fieldsToUpdate,
},
{
new: true,
Expand Down
10 changes: 9 additions & 1 deletion src/resolvers/Organization/advertisements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { MAXIMUM_FETCH_LIMIT } from "../../constants";
export const advertisements: OrganizationResolvers["advertisements"] = async (
parent,
args,
context,
) => {
const parseGraphQLConnectionArgumentsResult =
await parseGraphQLConnectionArguments({
Expand Down Expand Up @@ -73,12 +74,19 @@ export const advertisements: OrganizationResolvers["advertisements"] = async (
.exec(),
]);

const advertisements = objectList.map(
(advertisement: InterfaceAdvertisement) => ({
...advertisement,
mediaUrl: `${context.apiRootUrl}${advertisement.mediaUrl}`,
}),
);

return transformToDefaultGraphQLConnection<
ParsedCursor,
InterfaceAdvertisement,
InterfaceAdvertisement
>({
objectList,
objectList: advertisements,
parsedArgs,
totalCount,
});
Expand Down
2 changes: 1 addition & 1 deletion src/typeDefs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const types = gql`
type AdvertisementsConnection {
edges: [AdvertisementEdge]
pageInfo: ConnectionPageInfo
pageInfo: DefaultConnectionPageInfo!
totalCount: Int
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/generatedGraphQLTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export type AdvertisementType =
export type AdvertisementsConnection = {
__typename?: 'AdvertisementsConnection';
edges?: Maybe<Array<Maybe<AdvertisementEdge>>>;
pageInfo?: Maybe<ConnectionPageInfo>;
pageInfo: DefaultConnectionPageInfo;
totalCount?: Maybe<Scalars['Int']['output']>;
};

Expand Down Expand Up @@ -3639,7 +3639,7 @@ export type AdvertisementEdgeResolvers<ContextType = any, ParentType extends Res

export type AdvertisementsConnectionResolvers<ContextType = any, ParentType extends ResolversParentTypes['AdvertisementsConnection'] = ResolversParentTypes['AdvertisementsConnection']> = {
edges?: Resolver<Maybe<Array<Maybe<ResolversTypes['AdvertisementEdge']>>>, ParentType, ContextType>;
pageInfo?: Resolver<Maybe<ResolversTypes['ConnectionPageInfo']>, ParentType, ContextType>;
pageInfo?: Resolver<ResolversTypes['DefaultConnectionPageInfo'], ParentType, ContextType>;
totalCount?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
Expand Down
49 changes: 49 additions & 0 deletions tests/resolvers/Mutation/updateAdvertisement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,55 @@ describe("resolvers -> Mutation -> updateAdvertisement", () => {
expect(advertisement).toEqual({ advertisement: expectedAdvertisement });
});

it(`updates the advertisement without media and returns it`, async () => {
const { requestContext } = await import("../../../src/libraries");

vi.spyOn(requestContext, "translate").mockImplementationOnce(
(message: string) => `Translated ${message}`,
);

const args: MutationUpdateAdvertisementArgs = {
input: {
_id: testAdvertisement._id,
name: "New Advertisement Name",
type: "POPUP",
},
};

const context = { userId: testSuperAdmin?.id };

const updateAdvertisementPayload = await updateAdvertisementResolver?.(
{},
args,
context,
);
const advertisement = updateAdvertisementPayload || {};

const updatedTestAdvertisement = await Advertisement.findOne({
_id: testAdvertisement._id,
}).lean();

let expectedAdvertisement;

if (!updatedTestAdvertisement) {
console.error("Updated advertisement not found in the database");
} else {
expectedAdvertisement = {
_id: updatedTestAdvertisement._id.toString(), // Ensure _id is converted to String as per GraphQL schema
name: updatedTestAdvertisement.name,
organizationId: updatedTestAdvertisement.organizationId,
mediaUrl: updatedTestAdvertisement.mediaUrl,
type: updatedTestAdvertisement.type,
startDate: updatedTestAdvertisement.startDate,
endDate: updatedTestAdvertisement.endDate,
createdAt: updatedTestAdvertisement.createdAt,
updatedAt: updatedTestAdvertisement.updatedAt,
creatorId: updatedTestAdvertisement.creatorId,
};
}
expect(advertisement).toEqual({ advertisement: expectedAdvertisement });
});

it(`updates the advertisement media with unsupported file type`, async () => {
const { requestContext } = await import("../../../src/libraries");

Expand Down
3 changes: 3 additions & 0 deletions tests/resolvers/Organization/advertisements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe("resolvers -> Organization -> Advertisement", () => {
},
{},
);
const context = { apiRootUrl: undefined };

expect(connection).toEqual({
edges: [
Expand All @@ -94,13 +95,15 @@ describe("resolvers -> Organization -> Advertisement", () => {
node: {
...testAdvertisement2,
_id: testAdvertisement2?._id.toString(),
mediaUrl: `${context.apiRootUrl}${testAdvertisement2.mediaUrl}`,
},
},
{
cursor: testAdvertisement1?._id.toString(),
node: {
...testAdvertisement1,
_id: testAdvertisement1?._id.toString(),
mediaUrl: `${context.apiRootUrl}${testAdvertisement1.mediaUrl}`,
},
},
],
Expand Down

0 comments on commit a7fa6e3

Please sign in to comment.