-
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.
- Loading branch information
1 parent
da5124d
commit 916e746
Showing
5 changed files
with
56 additions
and
17 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,36 @@ | ||
import { Model } from 'mongoose'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { UserV2 } from 'twitter-api-v2'; | ||
import { Author, AuthorDocument } from 'src/schemas/author.schema'; | ||
import { mapAuthor } from 'src/utils'; | ||
|
||
@Injectable() | ||
export class AuthorsService { | ||
constructor( | ||
@InjectModel(Author.name) private authorModel: Model<AuthorDocument>, | ||
) {} | ||
|
||
async createOrUpdate( | ||
authorRaw: UserV2, | ||
bitsongAddress: string, | ||
): Promise<string> { | ||
const author: Author = mapAuthor(authorRaw, bitsongAddress); | ||
|
||
try { | ||
const oldAuthorModel = await this.authorModel.findOneAndUpdate( | ||
{ authorId: author.authorId }, | ||
{ address: author.address }, | ||
{ new: true }, | ||
); | ||
|
||
return oldAuthorModel._id.toString(); | ||
} catch (error) { | ||
console.error("Author doesn't exist: ", error); | ||
|
||
const authorModel = await new this.authorModel(author).save(); | ||
|
||
return authorModel._id.toString(); | ||
} | ||
} | ||
} |
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