|
1 | 1 | import {
|
2 |
| - db, FieldValue, likeNFTBookCollection, likeNFTBookUserCollection, userCollection, |
| 2 | + db, |
| 3 | + FieldValue, |
| 4 | + likeNFTBookCollection, |
| 5 | + likeNFTBookUserCollection, |
| 6 | + likeNFTCollectionCollection, |
| 7 | + userCollection, |
3 | 8 | } from '../../firebase';
|
4 | 9 | import { migrateLikerLandEvmWallet } from '../../liker-land';
|
5 | 10 |
|
@@ -97,6 +102,53 @@ async function migrateLikerId(likeWallet:string, evmWallet: string) {
|
97 | 102 | }
|
98 | 103 | }
|
99 | 104 |
|
| 105 | +export async function migrateBookClassId(likeClassId:string, evmClassId: string) { |
| 106 | + try { |
| 107 | + // TODO: verify evmClassId contains information about likeClassId |
| 108 | + const res = await db.runTransaction(async (t) => { |
| 109 | + const migratedClassIds: string[] = []; |
| 110 | + const migratedCollectionIds: string[] = []; |
| 111 | + const [bookListingDoc, collectionQuery] = await Promise.all([ |
| 112 | + t.get(likeNFTBookCollection.doc(likeClassId).get()), |
| 113 | + t.get(likeNFTCollectionCollection.where('classIds', 'array-contains', likeClassId).limit(100)), |
| 114 | + ]); |
| 115 | + if (bookListingDoc.exists) { |
| 116 | + const { evmClassId: existingEvmClassId } = bookListingDoc.data(); |
| 117 | + if (!existingEvmClassId) { |
| 118 | + t.update(bookListingDoc.ref, { evmClassId }); |
| 119 | + migratedClassIds.push(likeClassId); |
| 120 | + } |
| 121 | + } |
| 122 | + collectionQuery.docs.forEach((doc) => { |
| 123 | + const { classIds } = doc.data(); |
| 124 | + const index = classIds.indexOf(likeClassId); |
| 125 | + if (index >= 0) { |
| 126 | + classIds[index] = evmClassId; |
| 127 | + t.update(doc.ref, { classIds }); |
| 128 | + migratedCollectionIds.push(doc.id); |
| 129 | + } |
| 130 | + }); |
| 131 | + return { |
| 132 | + migratedClassIds, |
| 133 | + migratedCollectionIds, |
| 134 | + }; |
| 135 | + }); |
| 136 | + const { |
| 137 | + migratedClassIds, |
| 138 | + migratedCollectionIds, |
| 139 | + } = res; |
| 140 | + return { |
| 141 | + error: null, |
| 142 | + migratedClassIds, |
| 143 | + migratedCollectionIds, |
| 144 | + }; |
| 145 | + } catch (error) { |
| 146 | + // eslint-disable-next-line no-console |
| 147 | + console.error(error); |
| 148 | + return { error: (error as Error).message }; |
| 149 | + } |
| 150 | +} |
| 151 | + |
100 | 152 | export async function migrateLikeUserToEvmUser(likeWallet: string, evmWallet: string) {
|
101 | 153 | const [
|
102 | 154 | { error: migrateLikerIdError, likerId },
|
|
0 commit comments