-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from stoqey/notifications
fix(notifications) add notifications, badge, fix fetchRates
- Loading branch information
Showing
11 changed files
with
336 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// createBadgeByModel {owner, model} | ||
// updateBadgeCount {id or owner + model, count+-} | ||
|
||
import { Badge, BadgeModel } from "./Badge.model"; | ||
|
||
import { CouchbaseConnection } from "couchset"; | ||
import { awaitTo } from "couchset/dist/utils"; | ||
import { isEmpty } from "lodash"; | ||
|
||
export const createBadgeByModel = async (owner: string, model: string = owner): Promise<Badge | null> => { | ||
try { | ||
const [, existingBadge] = await awaitTo(BadgeModel.pagination({ | ||
where: { | ||
owner, | ||
model | ||
} | ||
})); | ||
|
||
if (isEmpty(existingBadge) || !existingBadge) { | ||
return await BadgeModel.create({ | ||
owner, | ||
model, | ||
count: 0 | ||
}); | ||
} | ||
return existingBadge && existingBadge[0] | ||
} catch (error) { | ||
console.log("error creating badge", error); | ||
return null; | ||
} | ||
} | ||
|
||
export const getBadges = async (owner: string, models: string[]): Promise<Badge[] | null> => { | ||
try { | ||
|
||
const bucket = CouchbaseConnection.Instance.bucketName; | ||
|
||
const query = ` | ||
SELECT * | ||
FROM \`${bucket}\` badge | ||
WHERE badge._type = "${Badge.name}" | ||
AND badge.owner = "${owner}" | ||
AND badge.model in ${JSON.stringify(models)}; | ||
`; | ||
|
||
const [errorFetching, data] = await awaitTo(BadgeModel.customQuery( | ||
{ | ||
query: query, | ||
params: [owner, models], | ||
limit: 1000 | ||
} | ||
)); | ||
|
||
if (errorFetching) { | ||
throw errorFetching; | ||
} | ||
|
||
const [existingBadge = []] = data; | ||
/** | ||
const createNewBadges = models.map(async (model) => { | ||
return await createBadgeByModel(owner, model); | ||
} | ||
const badges = await Promise.all(createNewBadges); | ||
*/ | ||
|
||
if (isEmpty(existingBadge) || !existingBadge) { | ||
return null; | ||
} | ||
|
||
return existingBadge.map((x: any) => BadgeModel.parse(x.badge)); | ||
|
||
} catch (error) { | ||
console.log("error getting badge", error); | ||
return null; | ||
} | ||
} | ||
|
||
export const updateBadgeCount = async (owner: string, model: string, count: number) => { | ||
try { | ||
// find badge | ||
const [errorBadge, badge] = await awaitTo(createBadgeByModel(owner, model)); | ||
if (errorBadge) { | ||
throw errorBadge; | ||
} | ||
if (!badge || isEmpty(badge)) { | ||
throw new Error("error updating badge count"); | ||
} | ||
const currentCount = badge.count || 0; | ||
const newBadgeCount = currentCount + count; | ||
badge.count = newBadgeCount < 0 ? 0 : newBadgeCount; | ||
|
||
return await BadgeModel.updateById(badge.id, badge); | ||
} catch (error) { | ||
console.log("error updating badge count", error); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {Field, InputType, Model, ObjectType} from 'couchset'; | ||
|
||
@InputType('BadgeInput') | ||
@ObjectType() | ||
export class Badge { | ||
@Field(() => String, {nullable: true, description: 'ID of the badge'}) | ||
id = ''; | ||
|
||
@Field(() => String, {nullable: true, description: 'The owner of the account'}) | ||
owner = ''; | ||
|
||
@Field(() => String, {nullable: true, description: 'The model for this badge'}) | ||
model = ''; | ||
|
||
@Field(() => Number, {nullable: true, description: 'Counts'}) | ||
count = 0; | ||
} | ||
|
||
export const BadgeModel = new Model(Badge.name, {graphqlType: Badge}); | ||
|
||
// automatic | ||
|
||
export const { | ||
resolver: BadgeDefaultResolver, // there's going to be other custom resolvers | ||
pagination: BadgePagination, | ||
client: BadgeClient, | ||
modelKeys: BadgeModelKeys, | ||
} = BadgeModel.automate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Resolver, Query, Arg, UseMiddleware, Ctx } from "couchset"; | ||
import { log } from "roadman"; | ||
import { Badge } from "./Badge.model"; | ||
import { getBadges } from "./Badge.methods"; | ||
import { ContextType, isAuth } from "@roadmanjs/auth"; | ||
import _get from "lodash/get"; | ||
import { isEmpty } from "lodash"; | ||
|
||
@Resolver() | ||
export class BadgeResolver { | ||
@Query(() => [Badge]) | ||
@UseMiddleware(isAuth) | ||
async getBadges( | ||
@Ctx() ctx: ContextType, | ||
@Arg("models",() => [String], { nullable: false }) models: string[] | ||
): Promise<Badge[] | null> { | ||
try { | ||
|
||
const owner = _get(ctx, 'payload.userId', ''); | ||
const badges = await getBadges(owner, models); | ||
if (!badges || isEmpty(badges)) { | ||
throw new Error("not found"); | ||
} | ||
return badges; | ||
} catch (error) { | ||
log("error getting Ad category", error); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Badge.model'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.