Skip to content

Commit

Permalink
Creator vitest (#3109)
Browse files Browse the repository at this point in the history
* added test for creator.ts

* Added test for creator.ts

Signed-off-by: NishantSinghhhhh <[email protected]>

* Added test for creator.ts

Signed-off-by: NishantSinghhhhh <[email protected]>

* Added test for creator.ts

Signed-off-by: NishantSinghhhhh <[email protected]>

* changes

Signed-off-by: NishantSinghhhhh <[email protected]>

* changes

Signed-off-by: NishantSinghhhhh <[email protected]>

---------

Signed-off-by: NishantSinghhhhh <[email protected]>
  • Loading branch information
NishantSinghhhhh authored Jan 31, 2025
1 parent 80fea2a commit 6683e62
Show file tree
Hide file tree
Showing 6 changed files with 501 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const config: Config = {
{
docs: {
sidebarPath: require.resolve("./sidebars.js"),
editUrl: ({ docPath }) => {
editUrl: ({ docPath }: { docPath: string }) => {
return `https://github.com/PalisadoesFoundation/talawa-api/edit/develop/docs/docs/${docPath}`;
},
},
Expand Down
204 changes: 204 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,195 @@ type FundCampaignsConnectionEdge {
node: FundCampaign
}

"""
Possible variants of the two-letter language code defined in ISO 639-1, part of the ISO 639 standard published by the International Organization for Standardization (ISO), to represent natural languages.
"""
enum Iso639Set1LanguageCode {
aa
ab
ae
af
ak
am
an
ar
as
av
ay
az
ba
be
bg
bi
bm
bn
bo
br
bs
ca
ce
ch
co
cr
cs
cu
cv
cy
da
de
dv
dz
ee
el
en
eo
es
et
eu
fa
ff
fi
fj
fo
fr
fy
ga
gd
gl
gn
gu
gv
ha
he
hi
ho
hr
ht
hu
hy
hz
ia
id
ie
ig
ii
ik
io
is
it
iu
ja
jv
ka
kg
ki
kj
kk
kl
km
kn
ko
kr
ks
ku
kv
kw
ky
la
lb
lg
li
ln
lo
lt
lu
lv
mg
mh
mi
mk
ml
mn
mr
ms
mt
my
na
nb
nd
ne
ng
nl
nn
no
nr
nv
ny
oc
oj
om
or
os
pa
pi
pl
ps
pt
qu
rm
rn
ro
ru
rw
sa
sc
sd
se
sg
si
sk
sl
sm
sn
so
sq
sr
ss
st
su
sv
sw
ta
te
tg
th
ti
tk
tl
tn
to
tr
ts
tt
tw
ty
ug
uk
ur
uz
ve
vi
vo
wa
wo
xh
yi
yo
za
zh
zu
}

"""
Possible variants of the two-letter country code defined in ISO 3166-1, part of the ISO 3166 standard published by the International Organization for Standardization (ISO), to represent countries, dependent territories, and special areas of geographical interest.
"""
Expand Down Expand Up @@ -1583,6 +1772,9 @@ input MutationCreateUserInput {
"""The sex assigned to the user at their birth."""
natalSex: UserNatalSex

"""Language code of the user's preferred natural language."""
naturalLanguageCode: Iso639Set1LanguageCode

"""Password of the user to sign in to the application."""
password: String!

Expand Down Expand Up @@ -1812,6 +2004,9 @@ input MutationSignUpInput {
"""The sex assigned to the user at their birth."""
natalSex: UserNatalSex

"""Language code of the user's preferred natural language."""
naturalLanguageCode: Iso639Set1LanguageCode

"""Password of the user to sign in to the application."""
password: String!

Expand Down Expand Up @@ -2029,6 +2224,9 @@ input MutationUpdateCurrentUserInput {
"""The sex assigned to the user at their birth."""
natalSex: UserNatalSex

"""Language code of the user's preferred natural language."""
naturalLanguageCode: Iso639Set1LanguageCode

"""Password of the user to sign in to the application."""
password: String

Expand Down Expand Up @@ -2249,6 +2447,9 @@ input MutationUpdateUserInput {
"""The sex assigned to the user at their birth."""
natalSex: UserNatalSex

"""Language code of the user's preferred natural language."""
naturalLanguageCode: Iso639Set1LanguageCode

"""Password of the user to sign in to the application."""
password: String

Expand Down Expand Up @@ -2976,6 +3177,9 @@ type User {
"""The sex assigned to the user at their birth."""
natalSex: UserNatalSex

"""Language code of the user's preferred natural language."""
naturalLanguageCode: Iso639Set1LanguageCode

"""
GraphQL connection to traverse through the organizations the user is a member of.
"""
Expand Down
98 changes: 98 additions & 0 deletions src/graphql/types/Advertisement/Advertisement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,110 @@ import {
AdvertisementAttachment,
type AdvertisementAttachment as AdvertisementAttachmentType,
} from "~/src/graphql/types/AdvertisementAttachment/AdvertisementAttachment";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";

type User = {
id: string;
isAdmin: boolean;
};

type ContextType = {
currentClient: {
isAuthenticated: boolean;
user: User;
};
drizzleClient: {
query: {
usersTable: {
findFirst: (params: { where: { id: string } }) => Promise<User>;
};
};
};
log: {
error: (message: unknown) => void;
};
};

export type Advertisement = typeof advertisementsTable.$inferSelect & {
attachments: AdvertisementAttachmentType[] | null;
};

export const Advertisement = builder.objectRef<Advertisement>("Advertisement");
export const resolveCreator = async (
parent: Advertisement,
_args: Record<string, never>,
ctx: ContextType,
) => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
message: "User is not authenticated",
extensions: {
code: "unauthenticated",
},
});
}

try {
// Add null check for creatorId before querying
if (!parent.creatorId) {
throw new TalawaGraphQLError({
message: "Associated creator not found for advertisement",
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["creatorId"], // Use argumentPath instead of path
},
],
},
});
}

const user = await ctx.drizzleClient.query.usersTable.findFirst({
where: { id: parent.creatorId }, // Now guaranteed to be string
});

if (!user) {
ctx.log.error(`User with id ${parent.creatorId} not found`);
throw new TalawaGraphQLError({
message: "User not found",
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["creatorId"], // Required by the type definition
},
],
},
});
}

if (
user.id !== ctx.currentClient.user.id &&
!ctx.currentClient.user.isAdmin
) {
throw new TalawaGraphQLError({
message: "User is not authorized",
extensions: {
code: "unauthorized_action",
},
});
}

return user;
} catch (error) {
if (error instanceof TalawaGraphQLError) {
throw error;
}
ctx.log.error(error);
throw new TalawaGraphQLError({
message: "Internal server error",
extensions: {
code: "unexpected",
},
});
}
};

Advertisement.implement({
description:
Expand Down
Loading

0 comments on commit 6683e62

Please sign in to comment.